Spring框架总结(三)

SpringIOC容器

一、创建对象

名称 描述
SpringIOC容器,是spring核心内容。 作用: 创建对象 & 处理对象的依赖关系
IOC容器创建对象 创建对象, 有几种方式
1) 调用无参数构造器
2) 带参数构造器
3) 工厂创建对象
工厂类,静态方法创建对象
工厂类,非静态方法创建对象

二、实战

事例一:构造函数是无参

  • 创建一个无参构造函数,需要三个部分

User


 1 public class User {
 2 
 3     public User() {
 4         System.out.println("======不带参构造器=========");
 5     }
 6 
 7     public User(String userId, String username) {
 8         System.out.println("======带参构造器=========");
 9         this.userId = userId;
10         this.username = username;
11     }
12 
13     public User(String userId, String username, String string) {
14         System.out.println("======带参构造器2=========");
15         this.userId = userId;
16         this.username = username;
17     }
18 
19     private String userId;
20     private String username;
21 
22     public String getUserId() {
23         return userId;
24     }
25 
26     public void setUserId(String userId) {
27         this.userId = userId;
28     }
29 
30     public String getUsername() {
31         return username;
32     }
33 
34     public void setUsername(String username) {
35         this.username = username;
36     }
37 
38     @Override
39     public String toString() {
40         return "User [userId=" + userId + ", username=" + username + "]";
41     }
42 
43 }

bean.xml


 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context
 9         http://www.springframework.org/schema/context/spring-context.xsd">
10 
11 
12     <!-- 无参构造函数的 -->
13     <bean id="user1" class="com.liuyang.constructor.User">
14     </bean>
15 </beans>

测试类


 1 public class App {
 2 
 3     ApplicationContext aContext = new ClassPathXmlApplicationContext(
 4             "com/liuyang/constructor/beans.xml");
 5 
 6     @Test
 7     public void test() {
 8         User user = (User) aContext.getBean("user");
 9         
10         System.out.println(user);
11     }
12 }

事例二:构造函数是带参

  • 更改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" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 带构造函数的 -->
    <bean id="user1" class="com.liuyang.constructor.User">
        <constructor-arg value="100" index="0" type="java.lang.String"></constructor-arg>
        <constructor-arg value="Jack" index="1" type="java.lang.String"></constructor-arg>
</bean>
</beans>

事例三:外部引用,定义一个字符串外部引入类中(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" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<!--定义一个字符串类型 相当于new String("whiteRabbit")-->
<bean id="str" class="java.lang.String">
        <constructor-arg value="whiteRabbit"></constructor-arg>
    </bean>
<bean id="user1" class="com.liuyang.constructor.User">
        <constructor-arg value="100" index="0" type="java.lang.String"></constructor-arg>
        <constructor-arg value="Jack" index="1" type="java.lang.String"></constructor-arg>
        <constructor-arg index="2" type="java.lang.String"
            ref="str"></constructor-arg>

    </bean>
</beans>

事例四:向类中插入值

  • 更改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" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 向对象中传值 -->
    <bean id="str" class="java.lang.String">
        <constructor-arg value="whiteRabbit"></constructor-arg>
    </bean>
    <bean id="user" class="com.liuyang.constructor.User">
        <constructor-arg index="0" type="java.lang.String"
            value="100"></constructor-arg>
        <constructor-arg index="1" type="java.lang.String"
            ref="str"></constructor-arg>
    </bean>
</beans>

事例五:工厂创建对象和静态调用

  • 1、user不变

  • 2、创建类ObjectFactory


 1 public class ObjectFactory {
 2 
 3     public User getInstance() {
 4 
 5         return new User("100", "=====实例化一个对象=========");
 6 
 7     }
 8 
 9     public static User getStaticInstance() {
10         return new User("101", "=====静态方法调用=========");
11 
12     }
13 
14 }

  • 3、更改bean.xml为如下

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context
 9         http://www.springframework.org/schema/context/spring-context.xsd">
10 
11 
12     <!-- 实例化一个对象 -->
13     <bean id="oFactory" class="com.liuyang.static1.constructor.ObjectFactory">
14     </bean>
15     <bean id="user" factory-bean="oFactory" factory-method="getInstance"></bean>
16 
17     <!-- 静态方法调用 -->
18     <bean id="user2" class="com.liuyang.static1.constructor.ObjectFactory"
19         factory-method="getStaticInstance"></bean>
20 </beans>

  • 4、测试

 1 public class App {
 2 
 3     ApplicationContext aContext = new ClassPathXmlApplicationContext(
 4             "com/liuyang/static1/constructor/beans2.xml");
 5 
 6     @Test
 7     public void test() {
 8         User user = (User) aContext.getBean("user");
 9 
10         System.out.println(user);
11     }
12 
13     @Test
14     public void test2() {
15         User user2 = (User) aContext.getBean("user2");
16 
17         System.out.println(user2);
18     }
19 }

posted @ 2017-03-11 16:05  北极的大企鹅  阅读(257)  评论(0编辑  收藏  举报
阅读 - 79万