一、Spring属性注入介绍
1 创建对象时候,向类里面属性里面设置值
2 属性注入的方式介绍(三种方式)
(1)使用 set 方法注入
(2)使用有参数构造注入
(3)使用接口注入
3 在 spring 框架里面,支持前两种方式
(1)set 方法注入(重点)
(2)有参数构造注入
- 使用有参数构造注入属性
User.java
- package com.liuyanzhao.property;
- public class User {
- private String username;
- public User(String username) {
- this.username = username;
- }
- public void test1() {
- System.out.println(“User………”+username);
- }
- }
bean1.xml
- <bean id=“userId” class=“com.liuyanzhao.property.User”>
- <!–使用有参构造注入–>
- <constructor-arg name=“username” value=“张三”></constructor-arg>
- </bean>
TestIOC.java
- package com.liuyanzhao.property;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestIOC {
- @Test
- public void testUser() {
- //1、加载Spring配置文件,根据创建对象
- ApplicationContext context =
- new ClassPathXmlApplicationContext(“bean1.xml”);
- //2、得到配置创建的对象
- User user1 = (User) context.getBean(“userId”);
- user1.test1();
- }
- }
- 使用 set 方法注入属性(重点)
Book.java
- package com.liuyanzhao.property;
- public class Book {
- private String bookname;
- public void setBookname(String bookname) {
- this.bookname = bookname;
- }
- public void test1() {
- System.out.println(“book……….”+bookname);
- }
- }
bean1.xml
- <bean id=“bookId” class=“com.liuyanzhao.property.Book”>
- <!–使用 set 方法煮注入–>
- <property name=“bookname” value=“战国策”></property>
- </bean>
TestIOC.java
- package com.liuyanzhao.property;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestIOC {
- @Test
- public void testUser() {
- //1、加载Spring配置文件,根据创建对象
- ApplicationContext context =
- new ClassPathXmlApplicationContext(“bean1.xml”);
- //2、得到配置创建的对象
- Book book = (Book) context.getBean(“bookId”);
- book.test1();
- }
- }
二、注入对象类型属性(重点)
1 创建 service 类和 dao 类
(1)在 service 得到 dao 对象
2 具体实现过程
(1)在 service 里面把 dao 作为类型属性
(2)生成 dao 类型属性的 set 方法
3、代码如下
UserDao.java
- package com.liuyanzhao.ioc;
- public class UserDao {
- public void add() {
- System.out.println(“dao…..add…….”);
- }
- }
UserService
- package com.liuyanzhao.ioc;
- public class UserService {
- //1、定义 dao 类型属性
- private UserDao userDao;
- //2.生成 set 方法
- public void setUserDao(UserDao userDao) {
- this.userDao = userDao;
- }
- public void add() {
- System.out.println(“service…..add…..”);
- userDao.add();
- }
- }
bean1.xml
- <!–1、配置 service 和 dao 对象–>
- <bean id=“userDaoId” class=“com.liuyanzhao.ioc.UserDao”></bean>
- <bean id=“userServiceId” class=“com.liuyanzhao.ioc.UserService”>
- <!–注入 dao 对象
- name 属性值:service 类里面属性的名称
- 现在不要写 value,因为刚才是字符串,现在是对象
- ref 属性:dao 配置 bean 标签中 id 值
- —>
- <property name=“userDao” ref=“userDaoId”></property>
- </bean>
TestIOC.java
- package com.liuyanzhao.ioc;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestIOC {
- @Test
- public void testUser() {
- //1、加载Spring配置文件,根据创建对象
- ApplicationContext context =
- new ClassPathXmlApplicationContext(“bean1.xml”);
- //2、得到配置创建的对象
- UserService userService = (UserService) context.getBean(“userServiceId”);
- userService.add();
- }
- }
三、注入复杂类型属性
1 数组
2 list 集合
3 map 集合
4 properties 类型
代码如下
Person.java
- package com.liuyanzhao.property;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
- public class Person {
- private String[] arrs;
- private List<String> list;
- private Map<String,String> map;
- private Properties properties;
- public void setArrs(String[] arrs) {
- this.arrs = arrs;
- }
- public void setList(List<String> list) {
- this.list = list;
- }
- public void setMap(Map<String, String> map) {
- this.map = map;
- }
- public void setProperties(Properties properties) {
- this.properties = properties;
- }
- public void test1() {
- System.out.println(“arrs:”+arrs);
- System.out.println(“list:”+list);
- System.out.println(“map:”+map);
- System.out.println(“properties:”+properties);
- }
- }
bean1.xml
- <bean id=“personId” class=“com.liuyanzhao.property.Person”>
- <!–数组–>
- <property name=“arrs”>
- <list>
- <value>张三</value>
- <value>李四</value>
- <value>王五</value>
- </list>
- </property>
- <!–list–>
- <property name=“list”>
- <list>
- <value>张三</value>
- <value>李四</value>
- <value>王五</value>
- </list>
- </property>
- <!–map–>
- <property name=“map”>
- <map>
- <entry key=“aa” value=“Lucy”></entry>
- <entry key=“bb” value=“Tom”></entry>
- <entry key=“cc” value=“Jerry”></entry>
- </map>
- </property>
- <!–properties–>
- <property name=“properties”>
- <props>
- <prop key=“driverclass”>com.mysql.jdbc.Driver</prop>
- <prop key=“username”>root</prop>
- </props>
- </property>
- </bean>
TestIOC
- package com.liuyanzhao.property;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestIOC {
- @Test
- public void testUser() {
- //1、加载Spring配置文件,根据创建对象
- ApplicationContext context =
- new ClassPathXmlApplicationContext(“bean1.xml”);
- //2、得到配置创建的对象
- Person person = (Person) context.getBean(“personId”);
- person.test1();
- }
- }
运行结果
arrs:[Ljava.lang.String;@1f021e6c
list:[张三, 李四, 王五]
map:{aa=Lucy, bb=Tom, cc=Jerry}
properties:{driverclass=com.mysql.jdbc.Driver, username=root}