@Component("bookDao")
public class BookDaoImpl implements BookDao {
}
@Component
public class BookServiceImpl implements BookService {
}
<context:component-scan base-package="nuc.kang.dao.impl"/>
<context:component-scan base-package="nuc.kang.service.impl"/>
或者
<context:component-scan base-package="nuc.kang"/>
<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="nuc.kang"/>
beans>
从xml到注解开发
@Configuration//代表applicationContext.xml中的配置文件的结构
@ComponentScan("nuc.kang")//配置文件中的扫描
public class SpringConfig {
}
Configuration
注解用于设定当前类为配置类ComponentScan
注解用于设定扫描路径,此注解只能添加一次,多个数据请用数组格式@ComponentScan("nuc.kang.service","nuc.kang.dao")
//加载配置文件初始化容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//加载配置类初始化容器
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
@Repository
@Scope("prototype")//singleton单例---prototype非单例
public class BookDaoImpl implements BookDao {
}
PostConstruct
、@PreDestroy
定义bean生命周期@Repository
@Scope("singleton")
public class BookDaoImpl implements BookDao {
public void save(){
System.out.println("book dao save...");
}
@PostConstruct
public void init(){
System.out.println("init...");
}
@PreDestroy
public void destroy(){
System.out.println("destroy...");
}
}
Autowired
注解开启自动装配模式(按类型)@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
public void save(){
System.out.println("book service save...");
bookDao.save();
}
}
注意:自动装配基于反射设计创建对象并暴力反射对应属性为私有属性初始化数据,因此无需提供setter方法
注意:自动装配建议使用无参构造方法创建对象(默认),如果不提供对应构造方法,请提供唯一的构造方法
使用@Qualifier注解开启指定名称装配bean
@Service
public class BookServiceImpl implements BookService {
@Autowired
@Qualifier("bookDao2")
private BookDao bookDao;
}
注意:@Qualifier注解无法单独使用,必须配合@Autowired
注解使用
使用@Value实现简单类型注入
@Repository("bookDao")
public class BookDaoImpl implements BookDao {
@Value("kanglei")
private String name;
}
PropertySource
注解加载properties文件@Configuration
@ComponentScan("nuc.kang")
@PropertySource({"jdbc.properties",""})
public class SpringConfig {
}
@Configuration
public class SpringConfig {
//1. 定义一个方法获得要管理的对象
//2.添加@Bean,表示当前方法的返回值是一个bean
@Bean
public DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/spring_db");
ds.setUsername("root");
ds.setPassword("root");
return ds;
}
}
public class JdbcConfig {
@Bean
public DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/spring_db");
ds.setUsername("root");
ds.setPassword("root");
return ds;
}
}
public class JdbcConfig {
@Bean
public DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/spring_db");
ds.setUsername("root");
ds.setPassword("root");
return ds;
}
}
@Configuration
@Import({JdbcConfig.class})
public class SpringConfig {
}
@Configuration
public class JdbcConfig {
@Bean
public DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/spring_db");
ds.setUsername("root");
ds.setPassword("root");
return ds;
}
}
ComponentScan
注解扫描配置类所在的包,加载对应的配置类信息@Configuration
@ComponentScan("nuc.kang.config","")
public class SpringConfig {
}
public class JdbcConfig {
@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;
@Bean
public DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(userName);
ds.setPassword(password);
return ds;
}
}
@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;
}
}