Spring explain (b)

1, three ways Spring loaded container

public class ServiceTest {
        public static void main(String[] args) {
            //Spring容器加载有3种方式

            //第一种:ClassPathXmlApplicationContext ClassPath类路径加载,指的就是classes路径
            //第一种:最常用,spring的配置文件路径以后就直接放在src
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

            //第二种方式:文件系统路径获得配置文件【绝对路径】
            //ApplicationContext context = new FileSystemXmlApplicationContext("C:\\Users\\Desktop\\IDEAWorkspace\\spring-01\\src\\com\\rookie\\beans.xml");

            //第三种方式:使用BeanFactory(了解)
            //String path = "C:\\Users\\Desktop\\IDEAWorkspace\\spring-01\\src\\com\\rookie\\beans.xml";
            //BeanFactory factory = new XmlBeanFactory(new FileSystemResource(path));
            //IUserService user = (IUserService) factory.getBean("userService");
            //user.add();

            IUserService user = (IUserService) context.getBean("userService");
            user.add();
        }
    }

The principle of creating an object inside Spring

a. xml file parsing, get the class name, id, property and so on.

b. Thoroughly reflection type used to create the object.

c. to create the object assignment.

2, BeanFactory and ApplicationContext Comparative

BeanFactory take lazy loading, Bean will initialize the first time getBean.

ApplicationContext is eagerly loaded on BeanFactory extended to provide more functionality.

  • Case presentation (in the first code on the basis of)

    public class UserServiceImpl implements UserService {
    
        private String name;
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public void add() {
            System.out.println("创建用户...." + name);
        }
    
        public UserServiceImpl(){
            System.out.println("UserServiceImpl() 调用了");
        }
    }
    public class ServiceTest {
    
        public static void main(String[] args) {
            //1.加载beans.xml 这个spring的配置文件,内部就会创建对象
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        }
    }

    Print Console log: UserServiceImpl () calls

    public class ServiceTest {
    
        public static void main(String[] args) {
            String path = "C:\\Users\\Desktop\\IDEAWorkspace\\spring-01\\src\\com\\rookie\\beans.xml";
    BeanFactory factory = new XmlBeanFactory(new FileSystemResource(path));
    
            // 要使调用空参构造可以放开这段代码即可
            // IUserService user = (IUserService) factory.getBean("userService");
        }
    }

    Console does not print the log, indicating an empty argument constructor is not called.

    Release note above code, you can call an empty argument constructor, console print log: UserServiceImpl () call.

2, Spring bean container assembly of three ways

The so-called assembly is to write a bean bean tag in xml.

<?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的三种方式,所谓的装配bean就是在xml写一个bean标签-->
    
    <!-- 第一种方式: new 实现类-->
    <bean id="userService1" class="com.example.demo.service.impl.UserServiceImpl">
        <property name="name" value="zhangsan"></property>
    </bean>

    <!-- 第二种方式:通过静态工厂方法 -->
    <bean id="userService2" class="com.example.demo.service.UserServiceFactory" factory-method="createUserService"/>

    <!--第三种方式:通过实例工厂方法 -->
    <bean id="factory2" class="com.example.demo.service.UserServiceFactory2"/>
    <bean id="userService3" factory-bean="factory2" factory-method="createUserService"/>
</beans>

What bean above test assembly methods, you need to comment other bean assembly methods.

The first has been talked about last time, and now they talk about the second three cases.

public class UserServiceFactory {
    public static UserService createUserService() {
        return new UserServiceImpl();
    }
}

=========================================================================================
public class UserServiceFactory2 {
    public UserService createUserService() {
        return new UserServiceImpl();
    }
}

Executes a test function

public class ServiceTest {

    public static void main(String[] args) {

        //new 对象
        ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService1 = (UserService) context1.getBean("userService1");
        userService1.add();


        //静态工厂
        ApplicationContext context2 = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService2 = (UserService) context2.getBean("userService2");
        userService2.add();


        //实例工厂
        ApplicationContext context3 = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService3 = (UserService) context3.getBean("userService3");
        userService3.add();

    }
}

Interested can see the results yourself. Three results have proved that the bean is instantiated by the Spring container management.

3, bean scope (master singleton, prototype)

category Explanation
singleton Spring IoC container there is only one instance of Bean, Bean manner in the presence of a single embodiment, the default value
prototype Each time the call Bean, returns a new instance of the container, i.e., each call to the getBean (), equivalent to the implementation new XxxBean ()
request Each HTTP request will create a new Bean, that scope applies only to the environment WebApplicationContext
session Share a same HTTP Session Bean, different Session use different Bean, applies only to the environment WebApplicationContext
globalSession Portlet applications for the general environment, the scope applies only to the environment WebApplicationContext

Case code demonstrates

bean.xml 文件
<?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="userService1" class="com.example.demo.service.impl.UserServiceImpl" scope="prototype">
    </bean>
</beans>
public class ServiceTest {

    public static void main(String[] args) {
       
        ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService1 = (UserService) context1.getBean("userService1");
        UserService userService2 = (UserService) context1.getBean("userService1");
        System.out.println(userService1);
        System.out.println(userService2);
    }
}

Console information is as follows:

com.example.demo.service.impl.UserServiceImpl@2a556333
com.example.demo.service.impl.UserServiceImpl@7d70d1b1

If you remove the scope bean.xml file = "prototype", print the following information:

com.example.demo.service.impl.UserServiceImpl@7a187f14
com.example.demo.service.impl.UserServiceImpl@7a187f14

Therefore Bean Spring IoC container exists in a single manner default! ! Configuration words themselves need to be configured according to a single or multiple Example Example

Guess you like

Origin www.cnblogs.com/miantiao312/p/11790089.html