spring-03 之IOC 属性注入(配置文件方式 附代码)
2018-01-27 19:47
766 查看
切记:不骄不躁 不浮不沉 思考着学习
属性注入: 1. 构造参数注入 2. get/set方法 3 接口注入 (Java中)
1. 构造参数注入 2. get/set方法(spring中主要有这两种方式 接下来则基于spring配置文件方式的形式 来演示)
构造参数注入:创建demo对象 java代码和xml配置如下
get/set方法:(1.注入普通类型属性
2.注入对象类型属性(属性的类型是一个class)
3.注入复杂类型属性(属性类型是list、map、properties))
一、用get/set方法注入普通类型属性
创建class类 通过spring 配置文件方式注入属性 对应的Java代码 xml配置如下
二、用get/set方法注入对象类型属性
[b] 应用场景:service中调用dao里面的一个方法时
[/b]
[b] 一般做法:是在service里面先new一个dao对象再使用dao对象调用对应的方法
;下面的方法中是在service中定义一个属性 类型为dao 具体实现过程如下:
[/b]
三用get/set方法注入复杂类型属性:创建person类 Java和xml文件如下
属性注入: 1. 构造参数注入 2. get/set方法 3 接口注入 (Java中)
1. 构造参数注入 2. get/set方法(spring中主要有这两种方式 接下来则基于spring配置文件方式的形式 来演示)
构造参数注入:创建demo对象 java代码和xml配置如下
package per.spring.beans; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo { public String name; //有參的構造方法 public Demo(String name) { this.name = name; } public static void main(String[] args) { //加载spring配置文件 根据文件 创建对象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //得到创建的对象 Demo demo = (Demo) context.getBean("demo"); System.out.println(demo.name); } }
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 属性注入的两种种方式 --> <!-- 有参构造注入 --> <bean id="demo" class="per.spring.beans.Demo"> //constructor-arg该标签的name属性的值是Demo类里面对应的属性名 <constructor-arg name="name" type="String"> <value>老王</value> </constructor-arg> </bean> </beans>
get/set方法:(1.注入普通类型属性
2.注入对象类型属性(属性的类型是一个class)
3.注入复杂类型属性(属性类型是list、map、properties))
一、用get/set方法注入普通类型属性
创建class类 通过spring 配置文件方式注入属性 对应的Java代码 xml配置如下
package per.spring.beans; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo2 { public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public static void main(String[] args) { //加载spring配置文件 根据文件 创建对象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //得到创建的对象 Demo2 demo2 = (Demo2) context.getBean("demo2"); System.out.println(demo2.name); } }
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <!-- get set 方法注入 --> <bean id="demo2" class="per.spring.beans.Demo2"> //properties标签中的name属性的值是Demo2类里面的属性名称 value的值是给该属性注入的值 <property name="name" value="老王"></property> </bean> </beans>
二、用get/set方法注入对象类型属性
[b] 应用场景:service中调用dao里面的一个方法时
[/b]
[b] 一般做法:是在service里面先new一个dao对象再使用dao对象调用对应的方法
;下面的方法中是在service中定义一个属性 类型为dao 具体实现过程如下:
[/b]
package per.spring.service; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import per.spring.dao.BookDao; public class BookService { //对象类型属性 BookDao为一个class private BookDao bookDao; public BookDao getBookDao() { return bookDao; } public void setBookDao(BookDao bookDao) { this.bookDao = bookDao; } public void add() { System.out.println("service......"); } /** * //测试service调用到是否成功 */ @Test public void testA() { //加载配置文件 创建对象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //得到创建的对象 BookService bookService = (BookService) context.getBean("bookService"); //调用dao的add方法 bookService.bookDao.add(); } }
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- get set 方法注入 --> <!-- 注入对象类型的属性 --> <bean id="bookService" class="per.spring.service.BookService"> <!-- 下面那个标签里面 name的值:是注入属性的名称 ref的值:是对应对象的bean标签的id值 --> <property name="bookDao" ref="bookDao"></property> </bean> <bean id="bookDao" class="per.spring.dao.BookDao"></bean> </beans>
三用get/set方法注入复杂类型属性:创建person类 Java和xml文件如下
package per.spring.beans; import java.util.List; import java.util.Map; import java.util.Properties; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Person { //list private List<String> array; //map private Map<String,String> map; //properties private Properties properties; public List<String> getArray() { return array; } public void setArray(List<String> array) { this.array = array; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } //测试方法 @Test public void testA() { //根据配置文件 创建对象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //获取对象 Person person = (Person) context.getBean("person"); System.out.println("list:"+person.array); System.out.println("map:"+person.map); System.out.println("properties:"+person.properties); } }
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 注入复杂类型的属性 list map properties --> <bean id="person" class="per.spring.beans.Person"> <!-- 注入list --> <property name="array"> <list> <value>小张</value> <value>小王</value> <value>小李</value> <value>小何</value> </list> </property> <!-- 注入map --> <property name="map"> <map> <entry key="1" value="小张"></entry> <entry key="2" value="小王"></entry> <entry key="3" value="小李"></entry> <entry key="4" value="小何"></entry> </map> </property> <!-- 注入properties --> <property name="properties"> <props> <prop key="driverclass">com.mysql.jdbc.Driver</prop> </props> </property> </bean> </beans>
相关文章推荐
- spring-04 之IOC 属性注入(注解方式 附代码)
- [Spring][Bean]Spring配置文件、属性注入、从IOC容器获取实例
- [3]Spring以配置文件的方式进行属性注入
- spring中属性文件和配置文件的注入方式
- Spring代码实例系列-09:通过Spring PropertyPlaceholderConfigurer将properties配置的属性注入到xml配置文件中
- Spring AMQP实例 以及Spring 配置文件动态注入属性使用实战
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- spring-自动加载配置文件\使用属性文件注入
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- spring-自动加载配置文件\使用属性文件注入
- Spring使用注解的方式注入属性时的xml文件配置
- Spring扫描配置文件的两种方式及获取配置文件属性参数的方式
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件二--获取注入的bean的二种方式
- lesson one:spring ioc,属性注入方式,实现自己的beanfactoy
- Spring IOC 注入方式详解 附代码
- Spring-----代码中使用注入的Properties配置属性
- spring的配置文件中bean的注入方式