@Component
bean的类上加注解@Component,value是否设置值,取决于用id获取还是根据class名获取,id获取就要给一个value对应id值。
xml中要先指定命名空间,在接component-scan开启包扫描,后面接要扫描那个路径下的包:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xxz"/>
</beans>
Spring提供@Component注解的三个衍生注解
@Controller:用于表现层bean定义
@Service:用于业务层bean定义
@Repository:用于数据层bean定义
@Configuration
用于设定当前类为配置类
作用是:spring对xml结构的简化,xml结构就是下面这块:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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"></beans>
@ComponentScan
用于设定扫描路径,此注解只能添加一次,多个数据用数组格式
也就是简化xml中的
<context:component-scan base-package="com.xxz"/>
bean模式:@Scope("singleton")
bean生命周期:@PostConstruct、@PreDestroy
public class BookDaoImpl implements BookDao {
@PostConstruct
public void init(){
System.out.println("book init ...");
}
@PreDestroy
public void destroy(){
System.out.println("book destory ...");
}
}
自动装配:@Autowired、@Qualifier
自动装配默认使用无参构造方法创建对象,并基于反射创建对象,因此无需提供set方法(之前的byType模式需要set方法,是因为有可能方法定义了private,框架考虑的是不能越俎代庖,现在为了方便都可以反射了)。
@Autowired是基于 byType模式的,需要bean唯一,当有多个相同类型的bean需要初始化时会报错,为了解决这个问题可以采用下面的@Qualifier注解,bean的类上里面配上bean的id,@Repository("bookDao"),@Qualifier配上对应的id
@Qualifier不能单独使用,要依赖于@Autowired,没有的话没有注入调用会报空指针。
值注入:@Value
读取配置文件:@PropertySource
多个路径书写为字符串数组格式,这里不支持使用通配符,会将*视为文件名的一部分,lasspath:可加可不加。
引入起来配置类:@Import
配置信息可能会有很多,所以我们用一个配置类专门管理,用@Import引入其他配置类。
注入返回方法对象:@Bean
表示当前方法的返回值是一个bean。
@Bean修饰的方法,形参根据类型自动装配
注解的综合用法:
定义bean:
@Repository
//@Component("bookDao")
@Scope("singleton")
public class BookDaoImpl implements BookDao {
@Value("${name}")
private String connectionNum;
@PostConstruct
public void init(){
System.out.println("book init ...");
}
@PreDestroy
public void destroy(){
System.out.println("book destory ...");
}
}
注入:
@Service
public class BookServiceImpl implements BookService {
@Autowired
//@Qualifier("bookDao1")
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void save() {
System.out.println("book service save ...");
bookDao.save();
}
}
@Configuration声明当前类为Spring配置类,
@Configuration
//设置bean扫描路径,多个路径书写为字符串数组格式
@ComponentScan({"com.xxz.service","com.xxz.dao"})//jdbc.properties放在的resources目录下
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class})
public class SpringConfig {
}
public class JdbcConfig {
//1.定义一个方法获得要管理的对象
@Value("com.mysql.jdbc.Driver")
private String driver;
@Value("jdbc:mysql://localhost:3306/spring_db")
private String url;
@Value("root")
private String userName;
@Value("root")
private String password;//当有依赖对象注入需求时,由于总配置类开启了包扫描,所以这里只需要给BookDao bookDao,框架就帮我们自动注入了。
@Bean
public DataSource dataSource(BookDao bookDao){
System.out.println(bookDao);
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(userName);
ds.setPassword(password);
return ds;
}
}
jdbc.properties:
name=888
启动类中采用注解容器:
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource);