✨博客主页:👉不会压弯的小飞侠
✨欢迎关注:👉点赞👍收藏⭐留言✒
✨系列专栏:👉spring专栏
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
✨欢迎大佬指正,一起学习!一起加油!
@TOC
一、注解开发定义Bean
1.使用@component定义Bean
package com.test.dao.impl;
import com.test.dao.BookDao;
import org.springframework.stereotype.Component;
@Component("dao")
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("BookDaoImpl...");
}
}
2.核心配置文件通过组件扫描加载Bean
<?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">
<context:component-scan base-package="com.test.dao.impl"></context:component-scan>
</beans>
3.案例
1.BookDao
package com.test.dao;
public interface BookDao {
void save();
}
2.BookDaoImpl
package com.test.dao.impl;
import com.test.dao.BookDao;
import org.springframework.stereotype.Component;
@Component("dao")
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("BookDaoImpl...");
}
}
3.BookService
package com.test.service;
public interface BookService {
void save();
}
4.BookServiceImpl
package com.test.service.impl;
import com.test.dao.BookDao;
import com.test.service.BookService;
import org.springframework.stereotype.Component;
@Component
public class BookServiceImpl implements BookService {
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void save() {
System.out.println("BookServiceImpl...");
}
}
5.applicationContext.xml
<?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">
<context:component-scan base-package="com.test.dao.impl"></context:component-scan>
<context:component-scan base-package="com.test.service"></context:component-scan>
</beans>
6.Test
import com.test.dao.BookDao;
import com.test.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao = context.getBean("dao",BookDao.class);
System.out.println(bookDao);
BookService bean = context.getBean(BookService.class);
System.out.println(bean);
}
}
/*
com.test.dao.impl.BookDaoImpl@5427c60c
com.test.service.impl.BookServiceImpl@6366ebe0
*/
二、纯注解开发
1.@Configuration注解代替了Spring的核心配置文件
@Configuration
2.@ComponentScan注解用于设定扫描路径
@ComponentScan("com.test")
3.加载配置类的初始化容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
4.案例
1.BookDao
package com.test.dao;
public interface BookDao {
void save();
}
2.BookDaoImpl
package com.test.dao.impl;
import com.test.dao.BookDao;
import org.springframework.stereotype.Component;
@Component("dao")
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("BookDaoImpl...");
}
}
3.BookService
package com.test.service;
public interface BookService {
void save();
}
4.BookServiceImpl
package com.test.service.impl;
import com.test.dao.BookDao;
import com.test.service.BookService;
import org.springframework.stereotype.Component;
@Component
public class BookServiceImpl implements BookService {
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void save() {
System.out.println("BookServiceImpl...");
}
}
5.SpringConfig
package com.test.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.test")
public class SpringConfig {
}
6.Test
import com.test.config.SpringConfig;
import com.test.dao.BookDao;
import com.test.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
BookDao bookDao = context.getBean("dao",BookDao.class);
System.out.println(bookDao);
BookService bean = context.getBean(BookService.class);
System.out.println(bean);
}
}
/*
com.test.dao.impl.BookDaoImpl@77e4c80f
com.test.service.impl.BookServiceImpl@255b53dc
*/
三、Bean的作用范围和生命周期
1.@Scope()定义Bean的作用范围
package com.test.dao.impl;
import com.test.dao.BookDao;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Scope("singleton")
@Component("dao")
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("BookDaoImpl...");
}
}
2.@PostConstruct和@PreDestroy定义Bean的生命周期
package com.test.dao.impl;
import com.test.dao.BookDao;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Scope("singleton")
@Component("dao")
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("BookDaoImpl...");
}
@PostConstruct
public void init(){
System.out.println("init...");
}
@PreDestroy
public void destroy(){
System.out.println("destroy...");
}
}
四、自动装配
1.使用@Autowired开启自动装配模式
package com.test.service.impl;
import com.test.dao.BookDao;
import com.test.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
/*set可以省略*/
/*public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}*/
public void save() {
System.out.println("BookServiceImpl...");
bookDao.save();
}
}
- 注意︰自动装配基于反射设计创建对象并暴力反射对应属性为私有属性初始化数据,因此无需提供setter方法
- 注意∶自动装配建议使用无参构造方法创建对象(默认),如果不提供对应构造方法,请提供唯一的构造方法
2.使用@Qualifier注解开启指定名称装配Bean
package com.test.service.impl;
import com.test.dao.BookDao;
import com.test.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class BookServiceImpl implements BookService {
@Autowired
@Qualifier("dao2")
private BookDao bookDao;
/*set可以省略*/
/*public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}*/
public void save() {
System.out.println("BookServiceImpl...");
bookDao.save();
}
}
3.使用@Value进行简单的注入
package com.test.dao.impl;
import com.test.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("dao1")
public class BookDaoImpl implements BookDao {
@Value("小马哥")
private String name;
public void save() {
System.out.println("BookDaoImpl..."+name);
}
}
4.使用@PropertySource注解加载properties文件
@PropertySource("jdbc.properties")
5.案例
jdbc.properties
name=Jack
1.BookDao
package com.test.dao;
public interface BookDao {
void save();
}
2.BookDaoImpl
package com.test.dao.impl;
import com.test.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("dao1")
public class BookDaoImpl implements BookDao {
/*@Value("小马哥")*/
@Value("${name}")
private String name;
public void save() {
System.out.println("BookDaoImpl..."+name);
}
}
3.BookDaoImpl2
package com.test.dao.impl;
import com.test.dao.BookDao;
import org.springframework.stereotype.Component;
@Component("dao2")
public class BookDaoImpl2 implements BookDao {
public void save() {
System.out.println("BookDaoImpl2...");
}
}
4.BookService
package com.test.service;
public interface BookService {
void save();
}
5.BookServiceImpl
package com.test.service.impl;
import com.test.dao.BookDao;
import com.test.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class BookServiceImpl implements BookService {
@Autowired
@Qualifier("dao1")
private BookDao bookDao;
/*set可以省略*/
/*public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}*/
public void save() {
System.out.println("BookServiceImpl...");
bookDao.save();
}
}
6.SpringConfig
package com.test.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan("com.test")
@PropertySource("jdbc.properties")
public class SpringConfig {
}
7.Test
import com.test.config.SpringConfig;
import com.test.dao.BookDao;
import com.test.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
BookService dao = context.getBean(BookService.class);
dao.save();
}
}
/*
BookServiceImpl...
BookDaoImpl...Jack
*/
五、第三方Bean管理
1.使用@Bean配置第三方bean
SpringConfig:
package com.jkj.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class SpringConfig {
//2.添加@Bean,表示当前方法的返回值是一个bean
@Bean
//1.定义一个方法获得要管理的对象
public DataSource dataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/spring");
ds.setUsername("root");
ds.setPassword("root");
return ds;
}
}
Test:
import com.jkj.config.SpringConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.sql.DataSource;
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
DataSource bean = context.getBean(DataSource.class);
System.out.println(bean);
}
}
2.将独立的配置类加入核心配置
1.导入式
使用@Import注解手动加入配置类到核心配置,此注解只能添加一次,多个数据使用数组格式。
案例:
SpringConfig:
package com.jkj.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import javax.sql.DataSource;
@Configuration
@Import(JdbcConfig.class)
public class SpringConfig {
}
JdbcConfig:
package com.jkj.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class JdbcConfig {
//2.添加@Bean,表示当前方法的返回值是一个bean
@Bean
//1.定义一个方法获得要管理的对象
public DataSource dataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/spring");
ds.setUsername("root");
ds.setPassword("root");
return ds;
}
}
2.扫描式(不推荐使用)
使用@ComponentScan注解扫描配置类所在的包,加载对应的配置类信息
案例:
SpringConfig:
package com.jkj.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import javax.sql.DataSource;
@Configuration
@ComponentScan("com.jkj.config")
public class SpringConfig {
}
JdbcConfig:
package com.jkj.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class JdbcConfig {
//2.添加@Bean,表示当前方法的返回值是一个bean
@Bean
//1.定义一个方法获得要管理的对象
public DataSource dataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/spring");
ds.setUsername("root");
ds.setPassword("root");
return ds;
}
}
六、第三方Bean依赖注入
1.简单依赖注入
public class JdbcConfig {
@Value("com.mysql.jdbc.Driver")
private String driver;
@Value("jdbc:mysql://localhost:3306/spring")
private String url;
@Value("root")
private String username;
@Value("root")
private String password;
//2.添加@Bean,表示当前方法的返回值是一个bean
@Bean
//1.定义一个方法获得要管理的对象
public DataSource dataSource(){
System.out.println(bookDao);
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
2.引用类型注入
引用类型注入只需要bean定义方法设置形参即可,容器会根据类型自动装配对象。
//2.添加@Bean,表示当前方法的返回值是一个bean
@Bean
//1.定义一个方法获得要管理的对象
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;
}
七、xml配置比对注解配置
定义bean
- xml配置:bean标签:id属性,class属性
- 注解:@Component,@controller,@Service,@Repository,@ComponentScan
设置依赖注入
- xml配置:setter注入(set方法),构造器注入(构造方法),引用/简单,自动装配
- 注解:@Autowired,@Qualifier,@Value
配置第三方bean
- xml配置:bean标签:静态工厂、实例工厂、FactoryBean
- 注解:@Bean
作用范围
- xml配置:scope属性
- 注解:@scope
生命周期
- xml配置:标准接口,init-method,destroy-method
- 注解:@PostConstructor,@PreDestroy