您的位置:首页 > 编程语言 > Java开发

spring-03 之IOC 属性注入(配置文件方式 附代码)

2018-01-27 19:47 766 查看
切记不骄不躁 不浮不沉 思考着学习






属性注入: 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>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐