SpringIOC的注解方式配置
创建maven项目
在pom.xml文件中加入依赖配置
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId> org.springframework </groupId>
<artifactId> spring-context </artifactId>
<version> 4.3.3.RELEASE </version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
编写DAO的接口和实现
接口
/**
* 模拟数据库处理
* @author MY
*
*/
public interface CustomerDao {
void save();
}
接口的实现
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Component;
import com.test.spring.ioc.annotationDemo.dao.CustomerDao;
@Component("customerDao1") /*默认的名字为类名首字母小写*/
/*@Scope(value="prototype")*/
public class CustomerDaoImpl implements CustomerDao{
@PostConstruct
public void init(){
System.out.println("CustomerDaoImpl对象的初始化方法");
}
@PreDestroy
public void destroy(){
System.out.println("CustomerDaoImpl对象的销毁方法");
}
public void save() {
System.out.println("保存到mysql数据库中!");
}
}
编写service的接口和实现
接口
public interface CustomerService {
void save();
void test();
}
实现类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.test.spring.ioc.annotationDemo.dao.CustomerDao;
import com.test.spring.ioc.annotationDemo.service.CustomerService;
@Service
public class CustomerServiceImpl implements CustomerService{
@Value("test")
private String name;
@Value("${url}")
private String url;
@Autowired
@Qualifier("customerDao1")
//@Resource(name="customerDao1")
private CustomerDao customerDao;
public void save() {
customerDao.save();
}
public void test() {
System.out.println(name);
System.out.println(url);
}
}
编写jdbc.properties文件
url=jdbc:mysql://localhost:3306/ssm
编写applicationContext.xmld的配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 使用spring的注解,需要导入名称空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/beans/spring-context.xsd
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 开启spring的注解扫描 base-package="com.test.spring.ioc.annotationDemo" 表示扫描 com.test.spring.ioc.annotationDemo 这个包下的所有类 -->
<context:component-scan base-package="com.test.spring.ioc.annotationDemo" />
</beans>
编写测试代码
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.spring.ioc.annotationDemo.dao.CustomerDao;
import com.test.spring.ioc.annotationDemo.service.CustomerService;
import jdk.nashorn.internal.ir.annotations.Ignore;
public class AnnotationTest {
private ApplicationContext ac;
@Before
public void init() {
ac = new ClassPathXmlApplicationContext("spring_ioc/applicationContextAnnotation.xml");
}
/**
* 测试@Component 注解
* 该注解不写值默认为类名首字母小写
* 指定名字
* @Component(value="名字")
*/
@Test
@Ignore
public void test1() {
CustomerDao dao = (CustomerDao) ac.getBean("customerDao1");
dao.save();
}
/**
* 测试spoce配置
* 默认为单例
* @Scope(value="prototype") 多例
*/
@Test
@Ignore
public void test2() {
for(int i=0;i<5;i++) {
CustomerDao dao = (CustomerDao) ac.getBean("customerDao1");
System.out.println(dao);
}
}
/**
* 测试指定初始化方法 和 销毁方法
* 使用注解
* @PostConstruct 指定初始化方法
* @PreDestroy 指定销毁方法
*/
@Test
@Ignore
public void test3() {
CustomerDao dao = (CustomerDao) ac.getBean("customerDao1");
dao.save();
//手动销毁对象
((AbstractApplicationContext)ac).registerShutdownHook();
}
/**
* 测试
* @Value("test")直接指定值
* @Value("${url}") 通过读取properties文件,进行值的指定
*/
@Test
@Ignore
public void test4() {
CustomerService dao = (CustomerService) ac.getBean("customerServiceImpl");
dao.test();
}
/**
* @Autowired
* 1)自动根据类型进入注入,直接赋值给变量(无需提供构造方法或者 setter 方法)
* 2)如果 Spring 环境中没有任何一个 CustomerDao 的类型,那么会注入失败
* 3)如果 Spring 环境中出现了多个 CustomerDao 的类型的对象,那么也会注入失败
*
* @Autowired
* @Qualifier("customerDao1")
* 这两个标签要一起使用
*/
@Test
public void test5() {
CustomerService dao = (CustomerService) ac.getBean("customerServiceImpl");
dao.test();
dao.save();
}
}
总结:
IOC 相关的注解
@Component : 创建对象 (普通的 Spring 项目)(加在类的上面)
@Repository: 和@Component 的作用是一样,创建对象(分层项目,Dao 层)
@Service:和@Component 的作用是一样,创建对象(分层项目,Service 层)
@Controller:和@Component 的作用是一样,创建对象(分层项目,Web 层) (加在类的上面)
@Bean:作用:把一个对象放入 Spring 的 IOC 容器
@Scope: 单例和多例(XML 方式里 scope 配置)(也可以与@Bean结合使用)
@PostConstruct: 初始化方法
@PreDestroy:销毁方法
依赖注入
@Value 作用:注入普通数据类型 如:@Value(“直接注入的值”) @Value(“${配置文件中的名}”)
@Autowired :自动根据类型注入
@Qualifier :指定注入的对象名称(注意:必须结合@Autowired使用)当@Autowired: 出现多个相同类型的对象的时候,可以使用@Qulifier 指定需要注入的对象名称。
@Resource:既能够根据类型自动注入,也可以根据对象名称注入(@Resource(name=”名字”))
@Autowired @Resource 注解的区别?
1.注入方式不一样
@Autowired :只能根据类型注入,如果需要根据名称注入,需要@Qualfier注解的配合。
而@Resource 既能根据类型注入,也可以根据对象名称注入
2.所属的标准不同
@Autowired :属于spring框架
@Resource :属于JavaEE标准
所以建议使用@Resource