一、注解
①、注解可以作用在类、方法、属性上
②、使用注解的目的:简化xml配置
一、Spring的IOC的注解入门
①、创建web项目,引入jar
②、编写类
public interface UserDao { public void save(); }
//注解中value属性值可以省略,默认值是首字母小写的类名称 @Component(value="userDao")//相当于 <bean id="userDao" class="com.xxx.spring.demo1.UserDaoImpl" public class UserDaoImpl implements UserDao { @Override public void save() { System.out.println("UserDaoImpl的save方法执行了"); } }
③、编写applicationContext.xml
Ⅰ、编写约束
通过spring-5.2.9.RELEASE-dist\spring-framework-5.2.9.RELEASE\docs\spring-framework-reference下的index.html
9.Appendix => 9.1.3.The context Schema
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> </beans>
Ⅱ、开启扫描
<!--开启组件扫描 若扫描多个包可用逗号隔开 或 使用上层目录 --> <context:component-scan base-package="com.xxx.spring.demo1" />
④、编写测试
@Test public void demo2() { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao dao=(UserDao) applicationContext.getBean("userDao"); dao.save(); }
以下四个注解功能相同,都可以用来创建bean实例:
@Component(修饰一个类,将这个类交给Spring管理)
有以下三个衍生注解(功能类似),也是修饰类
- @Service(用于对service层实现类进行标注,业务层)
- @Controller(用于对Controller实现类进行标注,web层)
- @Repository(用于对dao层实现类进行标注,持久层)
二、属性注入
①、编写Service,并交给Spring管理
public interface UserService { public void save(); }
@Service("userService") public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public void save() { System.out.println("UserServiceImpl中的save方法执行了"); userDao.save(); } }
②、编写Dao
//注解中value属性值可以省略,默认值是首字母小写的类名称 @Repository(value = "userDao") // 相当于 <bean id="userDao" class="com.xxx.spring.demo1.UserDaoImpl" public class UserDaoImpl implements UserDao { @Value("zhangsan")//普通属性 private String name; @Override public void save() { System.out.println("UserDaoImpl的save方法执行了" + name); } }
③、编写测试
@Test public void demo3() { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); UserService service=(UserService) applicationContext.getBean("userService"); service.save(); }
三、属性注解
①、普通属性:@Value
②、对象类型属性:
Ⅰ、@Autowired:根据对象类型完成属性注入,不需要添加set方法
@Autowired与 它修饰的属性的类型有关
@Autowired private UserDao userDao;
与spring管理的id或name无关
@Repository(value = "userDao") // 相当于 <bean id="userDao" class="com.xxx.spring.demo1.UserDaoImpl" public class UserDaoImpl implements UserDao {
Ⅱ、@Qualifier:根据属性名称进行注入,这个注解的使用要和@Autowired一起使用
@Autowired @Qualifier(value = "userDao") private UserDao userDao;
Ⅲ、@Resource:可以根据类型注入,也可以根据名称注入(JDK11以后完全移除了javax扩展导致不能使用@resource注解)
//根据类型 @Resource(type = UserDao.class) private UserDao userDao; //根据名称或ID @Resource(name = "userDao") private UserDao userDao;
四、其他注解
@Service("customerService")//<bean id="customerService" class="" init-method="init" destroy-method="destroy"> @Scope(scopeName = "singleton")//默认单例 /* * @Scope(scopeName = "prototype")//多列 * * @Scope(scopeName = "request") * * @Scope(scopeName = "session") * * @Scope(scopeName = "globalSession") */ public class CustomerService { @PostConstruct//相当于 init-method="init" public void init() { System.out.println("CustomerService init方法执行"); } public void save() { System.out.println("CustomerService save方法执行"); } @PreDestroy // 相当于 destroy-method="destroy" public void destroy() { System.out.println("CustomerService destroy方法执行"); } }
@Test public void demo1() { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml"); CustomerService service = (CustomerService) applicationContext.getBean("customerService"); service.save(); applicationContext.close(); }
五、完全注解开发
①、创建配置类替代XML配置文件 (使用注解@Configuration@ComponentScan)
@Configuration //作为配置类替代xml配置文件 @ComponentScan(basePackages = {"com.xxx.spring.demo3"}) //开启组件扫描 public class SpringConfig { }
②、创建Dao类,并交给Spring管理
public interface UserDao { public void find(); }
@Repository("userDao") public class UserDaoImpl implements UserDao { @Value("姓名") private String name; @Override public void find() { System.out.println("UserDaoImpl find方法执行" + name); } }
③、创建Service类,并交给Spring管理
public interface UserService { public void find(); }
@Service("userService") public class UserServiceImpl implements UserService { @Autowired @Qualifier("userDao") private UserDao userDao; @Override public void find() { System.out.println("UserServiceImpl find方法执行"); userDao.find(); } }
④、编写测试方法
@Test public void demo() { ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); UserService service=(UserService) context.getBean("userService"); service.find(); }
六、XML和注解整合开发
XML负责类(将类交给Spring管理),注解负责类中的属性(属性注入)
①、新建Service和Dao
public class ProductDao { @Value("商品名称") private String productName; public void find() { System.out.println("ProductDao find方法执行" + productName); } } public class OrderDao { @Value("订单编号") private String orderNo; public void find() { System.out.println("OrderDAO find方法执行" + orderNo); } } public class ProductService { @Autowired @Qualifier("productDao") private ProductDao productDao; @Resource(name = "orderDao") private OrderDao orderDao; public void find() { System.out.println("ProductService find方法执行了"); productDao.find(); orderDao.find(); } }
②、将Bean交给Spring管理
<!-- 将bean交给Spring管理 --> <bean id="productService" class="com.xxx.spring.demo4.ProductService"></bean> <bean id="productDao" class="com.xxx.spring.demo4.ProductDao"></bean> <bean id="orderDao" class="com.xxx.spring.demo4.OrderDao"></bean>
③、开启允许使用属性注解
<!--开启组件扫描 若扫描多个包可用逗号隔开 或 使用上层目录(针对类进行扫描) --> <!-- <context:component-scan base-package="com.xxx.spring.demo1,com.xxx.spring.demo2" /> --> <!-- 在没有扫描的情况下,使用属性注入的注解 --> <context:annotation-config/>
④、编写测试方法
@Test public void demo1() { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); ProductService productService=(ProductService) applicationContext.getBean("productService"); productService.find(); }