负责注入的注解,常见的包括四个:
package com.sunsplanter.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
@Value(value = "zhangsan")
private String name;
@Value("20")
private int age;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
/*@Value和setter方法结合
private String name;
private int age;
@Value("李四")
public void setName(String name) {
this.name = name;
}
@Value("30")
public void setAge(int age) {
this.age = age;
}
*/
/*@Value和构造方法结合
private String name;
private int age;
public User(@Value("隔壁老王") String name, @Value("33") int age) {
this.name = name;
this.age = age;
}
*/
}
配置文件开启包扫描:
<?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.sunsplanter.bean"/>
</beans>
测试程序:
@Test
public void testValue(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Object user = applicationContext.getBean("user");
System.out.println(user);
}
三种方法都可以正确注入简单类型.
目标: 存在两层: Service层和Dao层. 如前所述,UserService要控制UserDaoForMySQL, 必须要实例化一个UserDaoForMySQL的对象并注入到UserService类中定义的UserDaoForMySQL中.
配置Bean后Spring容器帮我们实例化了这个对象, 之前也学过了如何通过配置Bean标签注入. 本节的目标是通过注解完成自动注入.
Dao层接口UserDao:
package com.dao;
public interface UserDao {
void insert();
}
实现类UserDaoForMySQL:
package com.dao;
import org.springframework.stereotype.Repository;
@Repository //纳入bean管理
public class UserDaoForMySQL implements UserDao{
@Override
public void insert() {
System.out.println("正在向mysql数据库插入User数据");
}
}
Service层:
package com.Service;
import com.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service // 纳入bean管理
public class UserService {
@Autowired // 在属性上注入
private UserDao userDao;
// 没有提供构造方法和setter方法。
public void save(){
userDao.insert();
}
/* @Autowired和setter方法结合
private UserDao userDao;
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save(){
userDao.insert();
}*/
/*@Autowired和构造方法结合
private UserDao userDao;
public UserService(@Autowired UserDao userDao) {
this.userDao = userDao;
}
public void save(){
userDao.insert();
}*/
}
配置文件开启包扫描:
<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.dao,com.service"/>
beans>
测试程序:
@Test
public void testAutowired(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
UserService userService = applicationContext.getBean("userService", UserService.class);
userService.save();
}
解决办法:@Autowired注解和@Qualifier注解联合起来才可以根据名称进行装配,在@Qualifier注解中指定Bean名称。
将Service层代码增加一个@Qualifier注解,指定注入到userDaoForOracle:
private UserDao userDao;
@Autowired
@Qualifier("userDaoForOracle") // 这个是bean的名字。
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
Spring6不再支持JavaEE,它支持的是JakartaEE9。(之前所接触的所有的 javax.* 包名统一修改为 jakarta.*包名了。)
若使用Spring6, 引入:
<dependency>
<groupId>jakarta.annotationgroupId>
<artifactId>jakarta.annotation-apiartifactId>
<version>2.1.1version>
dependency>
使用:
UserDaoForOracle的名字为userDao,让这个Bean的名字和UserService类中的UserDao属性名一致:
package com.sunsplanter.dao;
import org.springframework.stereotype.Repository;
@Repository(value = "userDaoForOracle")
public class UserDaoForOracle implements UserDao{
@Override
public void insert() {
System.out.println("正在向Oracle数据库插入User数据");
}
}
package com.sunsplanter.service;
import com.sunsplanter.dao.UserDao;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Resource(name = "userDaoForOracle")
private UserDao userDao;
public void save(){
userDao.insert();
}
}
测试程序:
@Test
public void testResources(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
UserService userService = applicationContext.getBean("userService", UserService.class);
userService.save();
}
以上代码使用Resource注解根据name(bean id)进行注入.Resource的的name(bean id为)userDaoForOracle,且注解写在UserDao头上.那么就去寻找一个名为userDaoForOracle的UserDao实现类进行注入.
所谓的全注解开发就是不再使用spring配置文件spring.xml了。写一个配置类来代替配置文件. 其中配置类中使用
@configuration
@componentScan
等注解发挥spring.xml的作用
对于上例:
package com.sunsplanter.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan({"com.sunsplanter.dao", "com.sunsplanter.service"})
public class Spring6Configuration {
}
@Test
public void testNoXml(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Spring6Configuration.class);
UserService userService = applicationContext.getBean("userService", UserService.class);
userService.save();
}