@value
作用:将普通数据注入到类的字段
//2Value注入普通数据
@Value("${jdbcUrl}")
private String jdbcUrl;
@Value("${driverClass}")
private String driverClass;
@Value("${user}")
private String user;
@Value("${password}")
private String password;
--------------------------------------------------------------------
//
<context:property-placeholder location="classpath:jdbc.properties"/>
-------------------------------------------------------------------
jdbcUrl=jdbc:mysql:///spring
driverClass=com.mysql.jdbc.Driver
user=root
password=root
注册注解
1、@Component 普通的对象创建
2、@Repository dao层实现类的注解
3、@Service service层实现类的注解
4、@Controller controller层实现类的注解
以上四个注解在普通使用中是等效的,但在web项目中为了区分三层架构中不同层之间Bean的创建,为了避免注解使用的混乱,使用后三个注解进行区分。
在类上面添加这些注解后,相当于注册进容器,spring便会将这些类放入底层的map中进行相应的管理和分配。
注入注解
@Autowired
用于类的属性或者是方法上面,此注解会根据该属性的类型从bean管理器中将对象取出注入到该属性。
@Repository(value = "customerDao")
public class CustomerDaoImpl implements CustomerDao {
------------------------------------------------------
@Autowired
private CustomerDao customerDao;
此例子就是将CustomerImpl实例化入bean管理器,命名为customerDao,则在底层map中,customerDao为key,customerDaoImpl为value。
@Autowired就是将customerDao的value取出来注入到该属性。
除此之外和@Autowired具有相同作用的还有
@Resource
区别:
1.@Autowired是Spring的,@Resource是javax包下的。
2.@Autowired默认按类型匹配,@Resource默认按名称匹配
@Qualifier:
例如:当有两个实现类同时实现Dao接口时,如果使用autowired时,它就不知道该用哪一个实现类装配,这时候就可以将qualifier和它一起使用,这样名称和类型都匹配上就能注入。
@Autowired
@Qualifier("customerDao1") //关键是@Qualifier注解指定了名称
private CustomerDao customerDao;
Spring零配置
渐渐的,我们不再使用xml文件来配置Spring,从开始的@component等,直接在类上面进行配置,省区了很多麻烦。
@configration
作用:将带有该i注解的类作为spring的装配类。将以前xml文件的文职换成该类的class文件即可。
@ComponentScan
作用:注解在装配类的上面,用于扫描包中带有上面四个注册注解的类。
还有好几个重要的:
//将该类作为spring的配置类
@Configuration
//扫包注解
@ComponentScan(basePackages = {"edu.xja"})
//引入配置文件,这样可以快速使用配置里面的值
@PropertySource(value = "classpath:jdbc.properties")
//将其他配置类引入到此配置类,new Annotation时放入此类的Class文件即可装载几个类的配置。
@Import(JdbcConfig.class)
//导入xml文件中的配置
//@ImportResource("")
public class SpringConfig {
//主要是将第三方jar包内的类也放入容器进行管理,如果不取名字的话默认key为方法名,value为返回的对象
@Bean(name = "custmerDao")
CustomerDao getCustomerDao() {
return new CustomerDaoImpl();
}
}
Spring整合Junit(零配置)
再写测试类时,每次总是重复的写applicationContext和实现类总是很麻烦,这时我们可以用spring来整合junit
package edu.xja.service.impl;
import edu.xja.service.CustomerService;
import edu.xja.util.SpringConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.junit.jupiter.api.Assertions.*;
@ContextConfiguration(classes = SpringConfig.class)
@SpringJUnitConfig
class CustomerServiceImplTest {
@Autowired
private CustomerService customerService;
@Test
void save() {
//ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
//CustomerServiceImpl customerServiceImpl = (CustomerServiceImpl) applicationContext.getBean("customerServiceImpl");
customerService.save();
}
}