• 那是从何处传来的钟声呢?偶尔听到那钟声,平添一份喜悦与向往之情。

Spring IOC注入详解

后端 Nanait 12年前 (2012-11-15) 964次浏览 已收录 0个评论 扫描二维码
文章目录[隐藏]

一、Spring属性注入介绍

1 创建对象时候,向类里面属性里面设置值

 

2 属性注入的方式介绍(三种方式)

(1)使用 set 方法注入

(2)使用有参数构造注入

(3)使用接口注入

Spring IOC 注入详解

3 在 spring 框架里面,支持前两种方式

(1)set 方法注入(重点)

(2)有参数构造注入

 

  • 使用有参数构造注入属性

User.java

  1. package com.liuyanzhao.property;
  2. public class User {
  3.     private String username;
  4.     public User(String username) {
  5.         this.username = username;
  6.     }
  7.     public void test1() {
  8.         System.out.println(“User………”+username);
  9.     }
  10. }

bean1.xml

  1. <bean id=“userId” class=“com.liuyanzhao.property.User”>
  2.     <!–使用有参构造注入–>
  3.     <constructor-arg name=“username” value=“张三”></constructor-arg>
  4. </bean>

TestIOC.java

  1. package com.liuyanzhao.property;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class TestIOC {
  6.     @Test
  7.     public void testUser() {
  8.         //1、加载Spring配置文件,根据创建对象
  9.         ApplicationContext context =
  10.             new ClassPathXmlApplicationContext(“bean1.xml”);
  11.         //2、得到配置创建的对象
  12.         User user1 = (User) context.getBean(“userId”);
  13.         user1.test1();
  14.     }
  15. }

 

  • 使用 set 方法注入属性(重点)

Book.java

  1. package com.liuyanzhao.property;
  2. public class Book {
  3.     private String bookname;
  4.     public void setBookname(String bookname) {
  5.         this.bookname = bookname;
  6.     }
  7.     public void test1() {
  8.         System.out.println(“book……….”+bookname);
  9.     }
  10. }

bean1.xml

  1. <bean id=“bookId” class=“com.liuyanzhao.property.Book”>
  2.        <!–使用 set 方法煮注入–>
  3.        <property name=“bookname” value=“战国策”></property>
  4.    </bean>

TestIOC.java

  1. package com.liuyanzhao.property;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class TestIOC {
  6.     @Test
  7.     public void testUser() {
  8.         //1、加载Spring配置文件,根据创建对象
  9.         ApplicationContext context =
  10.             new ClassPathXmlApplicationContext(“bean1.xml”);
  11.         //2、得到配置创建的对象
  12.         Book book = (Book) context.getBean(“bookId”);
  13.         book.test1();
  14.     }
  15. }

 

二、注入对象类型属性(重点)

1 创建 service 类和 dao 类

(1)在 service 得到 dao 对象

 

2 具体实现过程

(1)在 service 里面把 dao 作为类型属性

(2)生成 dao 类型属性的 set 方法

 

3、代码如下

UserDao.java

  1. package com.liuyanzhao.ioc;
  2. public class UserDao {
  3.     public void add() {
  4.         System.out.println(“dao…..add…….”);
  5.     }
  6. }

UserService

  1. package com.liuyanzhao.ioc;
  2. public class UserService {
  3.     //1、定义 dao 类型属性
  4.     private UserDao userDao;
  5.     //2.生成 set 方法
  6.     public void setUserDao(UserDao userDao) {
  7.         this.userDao = userDao;
  8.     }
  9.     public  void add() {
  10.         System.out.println(“service…..add…..”);
  11.         userDao.add();
  12.     }
  13. }

bean1.xml

  1. <!–1、配置 service 和 dao 对象–>
  2.  <bean id=“userDaoId” class=“com.liuyanzhao.ioc.UserDao”></bean>
  3.  <bean id=“userServiceId” class=“com.liuyanzhao.ioc.UserService”>
  4.      <!–注入 dao 对象
  5.          name 属性值:service 类里面属性的名称
  6.                          现在不要写 value,因为刚才是字符串,现在是对象
  7.          ref 属性:dao 配置 bean 标签中 id 值
  8.      —>
  9.      <property name=“userDao” ref=“userDaoId”></property>
  10.  </bean>

TestIOC.java

  1. package com.liuyanzhao.ioc;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class TestIOC {
  6.     @Test
  7.     public void testUser() {
  8.         //1、加载Spring配置文件,根据创建对象
  9.         ApplicationContext context =
  10.             new ClassPathXmlApplicationContext(“bean1.xml”);
  11.         //2、得到配置创建的对象
  12.         UserService userService = (UserService) context.getBean(“userServiceId”);
  13.         userService.add();
  14.     }
  15. }

 

三、注入复杂类型属性

1 数组

2 list 集合

3 map 集合

4 properties 类型

代码如下

Person.java

  1. package com.liuyanzhao.property;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.Properties;
  5. public class Person {
  6.     private String[] arrs;
  7.     private List<String> list;
  8.     private Map<String,String> map;
  9.     private Properties properties;
  10.     public void setArrs(String[] arrs) {
  11.         this.arrs = arrs;
  12.     }
  13.     public void setList(List<String> list) {
  14.         this.list = list;
  15.     }
  16.     public void setMap(Map<String, String> map) {
  17.         this.map = map;
  18.     }
  19.     public void setProperties(Properties properties) {
  20.         this.properties = properties;
  21.     }
  22.     public void test1() {
  23.         System.out.println(“arrs:”+arrs);
  24.         System.out.println(“list:”+list);
  25.         System.out.println(“map:”+map);
  26.         System.out.println(“properties:”+properties);
  27.     }
  28. }

bean1.xml

  1. <bean id=“personId” class=“com.liuyanzhao.property.Person”>
  2.       <!–数组–>
  3.       <property name=“arrs”>
  4.           <list>
  5.               <value>张三</value>
  6.               <value>李四</value>
  7.               <value>王五</value>
  8.           </list>
  9.       </property>
  10.       <!–list–>
  11.       <property name=“list”>
  12.           <list>
  13.               <value>张三</value>
  14.               <value>李四</value>
  15.               <value>王五</value>
  16.           </list>
  17.       </property>
  18.       <!–map–>
  19.       <property name=“map”>
  20.           <map>
  21.               <entry key=“aa” value=“Lucy”></entry>
  22.               <entry key=“bb” value=“Tom”></entry>
  23.               <entry key=“cc” value=“Jerry”></entry>
  24.           </map>
  25.       </property>
  26.       <!–properties–>
  27.       <property name=“properties”>
  28.           <props>
  29.               <prop key=“driverclass”>com.mysql.jdbc.Driver</prop>
  30.               <prop key=“username”>root</prop>
  31.           </props>
  32.       </property>
  33.   </bean>

TestIOC

  1. package com.liuyanzhao.property;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class TestIOC {
  6.     @Test
  7.     public void testUser() {
  8.         //1、加载Spring配置文件,根据创建对象
  9.         ApplicationContext context =
  10.             new ClassPathXmlApplicationContext(“bean1.xml”);
  11.         //2、得到配置创建的对象
  12.         Person person = (Person) context.getBean(“personId”);
  13.         person.test1();
  14.     }
  15. }

运行结果

arrs:[Ljava.lang.String;@1f021e6c

list:[张三, 李四, 王五]

map:{aa=Lucy, bb=Tom, cc=Jerry}

properties:{driverclass=com.mysql.jdbc.Driver, username=root}

 

 


何处钟 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Spring IOC 注入详解
喜欢 (0)
[15211539367@163.com]
分享 (0)

您必须 登录 才能发表评论!