注解
就是一个类 使用方式 @使用名称
使用注解取代xml配置文件:
1 @component 取代<bean class = " ">
@componet("id") 取代<bean id = "id" class = " " >
注意:web开发中提供3个@component注解衍生注解(功能一样) 取代<bean>
@repository: dao层
@service:service层
@controller: web层
2依赖注入:给私有字段设置,也可以给setter方法设置
普通值 : @value("")
引用值: 方式3:
1:按照[类型]注入 默认 @Autowired
2:按照[名称]1注入 @Autowired @Qualifier("名称")
3:按照[名称]2注入 @resource("名称")
3 生命周期 初始化@PostConsturct 销毁@PreDestroy
4作用域 @Scope("prototype") eg多例
案例1
目标类
package com.itheima.a_ioc;
import org.springframework.stereotype.Component;
@Component("userServiceId")
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("annotation add user");
}
}
配置
<?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:p="http://www.springframework.org/schema/p"
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.itheima.a_ioc"></context:component-scan>
</beans>
测试
package com.itheima.a_ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestIoC {
@Test
public void demo02(){
String xmlPath = "com/itheima/a_ioc/beans.xml";
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
UserService userService = (UserService) applicationContext.getBean("userServiceId");
userService.addUser();
}
}
案列2
目标类
package com.itheima.g_annotation.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() ;
}
}
package com.itheima.g_annotation.web;
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();
}
}
package com.itheima.g_annotation.web;
import org.springframework.stereotype.Repository;
@Repository("studentDaoId")
public class StudentDaoImpl implements StudentDao {
@Override
public void save() {
System.out.println("dao");
}
}
2配置
<?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:p="http://www.springframework.org/schema/p"
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.itheima.g_annotation.web"></context:component-scan>
</beans>
测试
package com.itheima.g_annotation.web;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestWeb {
@Test
public void demo01(){
String xmlPath = "com/itheima/g_annotation/web/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath) ;
StudentAction studentAction = applicationContext.getBean("studentActionId", StudentAction.class) ;
studentAction.execute() ;
}
}