一、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}
本文链接:https://liuyanzhao.com/5620.html
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏