Spring-Boot实现对容器的IOC管理------Spring-Boot框架

package com.example.boot3.Config;

import com.example.boot3.bean.User;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@SpringBootConfiguration//这是一个SpringBoot配置类
//@Configuration//这是一个配置类
public class AppConfig
{
//    组件默认是单实例的
    @Scope("prototype")
    @Bean//替代Bean标签
    public User user(){//默认的Bean名字就是方法名字
        var user = new User();
        user.setName("Jack");
        user.setId(1L);
        return user;
    }
}
package com.example.boot3.Config;

import com.example.boot3.bean.User;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@SpringBootConfiguration//这是一个SpringBoot配置类
//@Configuration//这是一个配置类
public class AppConfig
{
//    组件默认是单实例的
    @Scope("prototype")
    @Bean//替代Bean标签
    public User user(){//默认的Bean名字就是方法名字
        var user = new User();
        user.setName("Jack");
        user.setId(1L);
        return user;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user" class="com.example.boot3.bean.User" scope="prototype">
        <property name="id" value="1"></property>
        <property name="name" value="Jack"></property>
    </bean>
    <bean id="cat" class="com.example.boot3.bean.Cat">
        <property name="id" value="2"></property>
        <property name="name" value="Rose"></property>
    </bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user" class="com.example.boot3.bean.User" scope="prototype">
        <property name="id" value="1"></property>
        <property name="name" value="Jack"></property>
    </bean>
    <bean id="cat" class="com.example.boot3.bean.Cat">
        <property name="id" value="2"></property>
        <property name="name" value="Rose"></property>
    </bean>
</beans>
package com.example.boot3;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.example")//改变包扫描路径
@SpringBootApplication(scanBasePackages = "com.example.boot3")//手动指定扫描包位置
public class Boot3Application
{
    public static void main(String[] args) {
        var ioc = SpringApplication.run(Boot3Application.class, args);
        for (String name : ioc.getBeanDefinitionNames()) {
            System.out.println(name);
        }
        Object user = ioc.getBean("user");
        Object user1 = ioc.getBean("user");
        System.out.println(user == user1);
    }
}
package com.example.boot3;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.example")//改变包扫描路径
@SpringBootApplication(scanBasePackages = "com.example.boot3")//手动指定扫描包位置
public class Boot3Application
{
    public static void main(String[] args) {
        var ioc = SpringApplication.run(Boot3Application.class, args);
        for (String name : ioc.getBeanDefinitionNames()) {
            System.out.println(name);
        }
        Object user = ioc.getBean("user");
        Object user1 = ioc.getBean("user");
        System.out.println(user == user1);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.atguigu</groupId>
    <artifactId>boot3-01</artifactId>
    <version>1.0-SNAPSHOT</version>

<!--    所有SpringBoot都必须继承-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.4</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
<!--    SpringBoot打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.atguigu</groupId>
    <artifactId>boot3-01</artifactId>
    <version>1.0-SNAPSHOT</version>

<!--    所有SpringBoot都必须继承-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.4</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
<!--    SpringBoot打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
package com.example.boot3.bean;

public class Cat
{
    private Long id;
    private String name;

    public Cat() {
    }

    public Cat(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
package com.example.boot3.bean;

public class Cat
{
    private Long id;
    private String name;

    public Cat() {
    }

    public Cat(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
package com.example.boot3.bean;

public class User
{
    private Long id;
    private String name;

    public User() {
    }

    public User(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
package com.example.boot3.bean;

public class User
{
    private Long id;
    private String name;

    public User() {
    }

    public User(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
package com.example.boot3.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController
{
    @GetMapping("/hello")
    public String Hello(){
        return "Hello";
    }
}
package com.example.boot3.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController
{
    @GetMapping("/hello")
    public String Hello(){
        return "Hello";
    }
}

猜你喜欢

转载自blog.csdn.net/2201_75960169/article/details/135388371