更多注入
基本类型即集合注入
package com.wanggs.service.impl;
import com.wanggs.dao.BookDao;
import com.wanggs.service.BookService;
import java.security.PrivateKey;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* Created by wanggs on 2017/7/7.
*/
public class BookServiceImpl implements BookService{
//之前的方式 接口 = 实现类;(多态)
//spring: 接口+set方法
//基本类型和集合注入
private BookDao bookDao;
private Integer id;
private String name;
private List<String> list;
private Set<Integer> set;
Map<String,String> map;
Properties properties;
public void setProperties(Properties properties) {
this.properties = properties;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setList(List<String> list) {
this.list = list;
}
public void setSet(Set<Integer> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setBookDao(BookDao bookDao ){
this.bookDao = bookDao;
}
public void saveBook() {
this.bookDao.addBook();
}
public BookDao getBookDao() {
return bookDao;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public List<String> getList() {
return list;
}
public Set<Integer> getSet() {
return set;
}
public Map<String, String> getMap() {
return map;
}
public Properties getProperties() {
return properties;
}
@Override
public String toString() {
return "BookServiceImpl{" +
"bookDao=" + bookDao +
", id=" + id +
", name='" + name + '\'' +
", list=" + list +
", set=" + set +
", map=" + map +
", properties=" + 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--id: bean 在容器的名称不能重复 -->
<!--class: 放入spring容器的包的完全限定名-->
<bean id="sayHello" class="com.wanggs.pojo.SayHello">
<property name="name" value="tom"/>
</bean>
<!--property 用于属性注入
name: 属性的名字==>>private BookDao bookDao;
ref: 引用另一个bean的id
-->
<bean id="bookService" class="com.wanggs.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"/>
<property name="id" value="001"/>
<property name="name" value="jack"/>
<!--lsit注入-->
<property name="list">
<list>
<value>Rose</value>
<value>Tom</value>
</list>
</property>
<!--set注入-->
<property name="set" >
<set>
<value>100</value>
<value>200</value>
</set>
</property>
<!--map注入-->
<property name="map">
<map>
<entry key="k1" value="v1"/>
<entry key="k2" value="v2"/>
</map>
</property>
<!--properties-->
<property name="properties">
<props>
<prop key="p1" >m1</prop>
<prop key="p2">m2</prop>
</props>
</property>
</bean>
<bean id="bookDao" class="com.wanggs.dao.impl.BookDaoImpl"/>
</beans>
package com.wanggs.service;
import com.wanggs.service.impl.BookServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Map;
import java.util.Set;
/**
* Created by wanggs on 2017/7/7.
*/
public class BookServiceTest {
public static void main(String[] args) {
// 读取配置文件,使用spring控制反转交给spring创建对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// "bookService"-->>即配置文件 (id: bean 在容器的名称)
//用的实现类接受便于测试
BookServiceImpl bookService = (BookServiceImpl) applicationContext.getBean("bookService");
bookService.saveBook();
Map<String, String> map = bookService.getMap();
Set<String> set = map.keySet();
for(String key : set){
System.out.println(key+"---->"+map.get(key));
}
System.out.println(bookService);
}
/**结果:
* DI add Book
k1---->v1
k2---->v2
BookServiceImpl{bookDao=com.wanggs.dao.impl.BookDaoImpl@4dfa3a9d, id=1, name='jack', list=[Rose, Tom], set=[100, 200], map={k1=v1, k2=v2}, properties={p2=m2, p1=m1}}
*/
}
构造方法注入
/**
* Created by wanggs on 2017/7/7.
*/
public class BookServiceImpl implements BookService {
private BookDao bookDao;
//构造方法注入
public BookServiceImpl(BookDao bookDao) {
this.bookDao = bookDao;
}
@Override
public void saveBook() {
this.bookDao.addBook();
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--构造方法注入-->
<bean id="bookService" class="com.wanggs.service.impl.BookServiceImpl">
<constructor-arg name="bookDao" ref="bookDao"/>
</bean>
<bean id="bookDao" class="com.wanggs.dao.impl.BookDaoImpl"/>
/**
* Created by wanggs on 2017/7/7.
*/
public class BookServiceTest {
public static void main(String[] args) {
//读取配置文件,使用spring控制反转转交给spring创建对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// "bookService"-->>即配置文件 (id: bean 在容器的名称)
BookService bookService = (BookService) applicationContext.getBean("bookService");
bookService.saveBook();
更多构造方法的注入
<!--更多构造方法注入-->
<bean id="bookService" class="com.wanggs.service.impl.BookServiceImpl">
<!-- 2.根据构造方法参数的索引,从0开始
<constructor-arg index="0" ref="bookDao"/>
-->
<!--3.根据构造方法的类型-->
<constructor-arg type="com.wanggs.dao.impl.BookDaoImpl" ref="bookDao"/>
</bean>
<bean id="bookDao" class="com.wanggs.dao.impl.BookDaoImpl"/>
<!-- 1. <!–构造方法注入–>
<bean id="bookService" class="com.wanggs.service.impl.BookServiceImpl">
<constructor-arg name="bookDao" ref="bookDao"/>
</bean>
<bean id="bookDao" class="com.wanggs.dao.impl.BookDaoImpl"/>
-->
自动注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--id: bean 在容器的名称不能重复 -->
<!--class: 放入spring容器的包的完全限定名-->
<bean id="sayHello" class="com.wanggs.pojo.SayHello">
<property name="name" value="tom"/>
</bean>
<!--自动注入-->
<!--byName 根据需要注入的属性名在容器内寻找名称相同的Bean,如果找到就注入,找不到就不注入-->
<bean id="bookService" class="com.wanggs.service.impl.BookServiceImpl" autowire="byName"/>
<bean id="bookDao" class="com.wanggs.dao.impl.BookDaoImpl"/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--id: bean 在容器的名称不能重复 -->
<!--class: 放入spring容器的包的完全限定名-->
<bean id="sayHello" class="com.wanggs.pojo.SayHello">
<property name="name" value="tom"/>
</bean>
<!--自动注入-->
<!--byType根据需要注入的属性类型在容器找类型相同的Bean,如果找到就注入,找不到就不注入,如果
找到多个类型相同的Bean,则抛出异常-->
<bean id="bookService" class="com.wanggs.service.impl.BookServiceImpl" autowire="byType"/>
<bean id="bookDao" class="com.wanggs.dao.impl.BookDaoImpl"/>
基于注解的Bean管理
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--基于Bean管理的IOC,自动扫描该路径下的类-->
<context:component-scan base-package="com.wanggs"></context:component-scan>
</beans>
代码
import java.security.PrivateKey;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* Created by wanggs on 2017/7/7.
*/
//@Repository
//@Component
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
/* //构造方法注入
public BookServiceImpl(BookDao bookDao) {
// System.out.println(name);
this.bookDao = bookDao;
}
*/
@Override
public void saveBook() {
this.bookDao.addBook();
}
}
基于DI的Spring单元测试
添加pom依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
测试
/**
* Created by wanggs on 2017/7/7.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class BookServiceTest {
@Autowired
private BookServiceImpl bookService;
@Test
public void addBook() throws Exception {
bookService.see();
}
}