注解:就是一个类,使用@注解名称,使用注解可以取代 xml配置文件。
案例一:
1.使用@Component取代<bean class="">
(@Component("id") 取代 <bean id="" class="">)
UserServiceImpl实现类:
import org.springframework.stereotype.Component;
@Component("UserServiceId")
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("g_annotation.a_ioc_user");
}
}
bean.xml:
<?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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--
注解取代其配置
<bean id="UserServiceId" class="com.spring.g_annotation.a_ioc.UserServiceImpl"></bean>
-->
<!--注解扫描,扫描含有注解的类-->
<context:component-scan base-package="com.spring.g_annotation.a_ioc"></context:component-scan>
</beans>
测试类:
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnnoIoc {
@Test
public void demo02() {
//从spring容器获得
//1.获取容器
String xmlPath = "com/spring/g_annotation/a_ioc/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
//2.获得内容,不需要自己new一个对象
UserService userService = (UserService) applicationContext.getBean("UserServiceId");
userService.addUser();
}
}
//运行结果:g_annotation.a_ioc_user
案例二:
2.在web开发,提供3个@Component注解衍生注解(功能一样)取代<bean class="">
@Repository :dao层
@Service:service层
@Controller:web层
3.依赖注入,给私有字段设置,也可以给setter方法设置
普通值:@Value("")
引用值:
方式1:按照【类型】注入
@Autowired
方式2:按照【名称】注入1
@Autowired
@Qualifier("名称")
方式3:按照【名称】注入2
@Resource("名称")
Dao层:
import org.springframework.stereotype.Repository;
@Repository("studentDaoId")
public class StudentDaoImpl implements StudentDao{
@Override
public void save() {
System.out.println("Dao");
}
}
Service层:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl implements StudentService{
private StudentDao studentDao;
@Autowired
@Qualifier("studentDaoId")
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public void addStudent() {
studentDao.save();
}
}
Web层:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller("StudentActionId")
public class StudentAction {
@Autowired//默认按照类型
private StudentService studentService;
public void execute() {
studentService.addStudent();
}
}
bean.xml:
<?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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--注解扫描,扫描含有注解的类-->
<context:component-scan base-package="com.spring.g_annotation.b_web"></context:component-scan>
</beans>
测试类:
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnnoIoc {
@Test
public void demo02() {
//从spring容器获得
//1.获取容器
String xmlPath = "com/spring/g_annotation/b_web/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
//2.获得内容,不需要自己new一个对象
StudentAction studentAction = (StudentAction) applicationContext.getBean("StudentActionId");
studentAction.execute();
}
}
//运行结果:Dao
案例三:
4.生命周期
初始化:@PostConstruct
销毁:@PreDestroy
5.作用域
@Scope("prototype") 多例
UserServiceImpl实现类:
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Service("UserServiceId")
//@Scope("prototype")//多例
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("g_annotation.c_other");
}
@PostConstruct
public void myInit(){
System.out.println("初始化");
}
@PreDestroy
public void myDestory(){
System.out.println("销毁");
}
}
bean.xml:(注解的xml文件只需要进行注解扫描的工作)
<?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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--注解扫描,扫描含有注解的类-->
<context:component-scan base-package="com.spring.g_annotation.c_other"></context:component-scan>
<!--<context:annotation-config></context:annotation-config>-->
</beans>
测试类:
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestOther {
@Test
public void demo02() {
//spring工厂
String xmlPath = "com/spring/g_annotation/c_other/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
UserService userService1 = applicationContext.getBean("UserServiceId", UserService.class);
//UserService userService2 = applicationContext.getBean("UserServiceId", UserService.class);
System.out.println(userService1);
//System.out.println(userService2);
((ClassPathXmlApplicationContext) applicationContext).close();
}
}
//运行结果:
/*
初始化
com.spring.g_annotation.c_other.UserServiceImpl@70be0a2b
销毁
*/
*注解使用的前提:添加命名空间,让spring扫描含有注解类