spring原始注释
spring是轻代码重配置的框架,配置比较繁琐,影响开发效率,注解开发替代xml配置文件可以简化配置,提供开发效率
主要是替代的配置
注解 | 说明 |
---|
@Component | 使用在类上用于实例化bean |
@Controller | 使用在web层类上用于实例化bean |
@Service | 使用在service层类上用于实例化bean |
@Repository | 使用在dao层类上用于实例化bean |
@Autowired | 使用在字段上用于根据类型依赖注入 |
@Qualifier | 结合@Autowired使用用于根据名称进行依赖注入 |
@Resource | 相当于@Autowired+@Qualifier按照名称进行注入 |
@Value | 注入普通属性 |
@Scope | 标注bean的作用范围 |
@PostConstruct | 使用在方法上标注该方法是bean的初始化方法(已废弃) |
@PreDestroy | 使用在方法上标注该方法是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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载外部的properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置组件扫描-->
<context:component-scan base-package="com.jentdata"></context:component-scan>
</beans>
dao层代码
import com.jentdata.dao.UserDao;
import org.springframework.stereotype.Repository;
/*<bean id="userDao" class="com.jentdata.dao.impl.UserDaoImpl"></bean>*/
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("save running 。。。");
}
}
service层代码
import com.jentdata.dao.UserDao;
import com.jentdata.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
/*<bean id="userService" class="com.jentdata.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>*/
@Service("userService")
public class UserServiceImpl implements UserService {
/*<property name="userDao" ref="userDao"></property>*/
@Autowired//按照数据类型从Spring容器中进行匹配
@Qualifier(value = "userDao")//按照id的名称从容器中进行匹配的,此处的@Qualifier要结合@Autowired一起使用
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public UserDao getUserDao() {
return userDao;
}
@Override
public void save() {
userDao.save();
}
}
spring新注解
上述无法用注解替代
- 非自定义的Bean的配置:
- 加载properties文件的配置:context:property-placeholder
- 组件扫描:context:component-scan
- 引入其他注解
新注解
注解 | 说明 |
---|
@Configuration | 用于指定当前类是一个Spring配置类,当创建容器时会从该类上加载注释 |
@ComponentScan | 指定Spring在初始化容器时要扫描的包 |
@Bean | 使用在方法上,标注将改方法的返回值存储到Spring容器中 |
@PropertySource | 用于加载.properties文件 |
@Import | 用于导入其他配置类 |
Java代码
相当于一个spring的xml配置文件
import org.springframework.context.annotation.*;
//标志该类是Spring的核心配置类
@Configuration
// <!-- <import resource="applicationContext.xml"></import>-->
@Import({DataSourceConfiguration.class})
public class SpringConfiguration {
}
新注解的应用
package com.jentdata.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
//<context:component-scan base-package="com.jentdata"></context:component-scan>
@ComponentScan("com.jentdata")
//<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean("dataSource")//spring会将当前方法的返回值以指定名称存储到spring容器当中
public DataSource getDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
}
调用
public static void main(String[] args) {
// ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
UserService userService = app.getBean(UserService.class);
userService.save();
}