Spring IOC注入详解

avatar 2017年08月04日21:41:52 1 2360 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此

一、Spring属性注入介绍


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



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

(1)使用set方法注入

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

(3)使用接口注入



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}





本文链接:https://liuyanzhao.com/5620.html
  • 微信
  • 交流学习,服务定制
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0