@Component
@Component("logger")
public class Logger {
作用:用于把当前类对象存入到Spring 容器中
属性: value :用于指定bean的id,当我们不写时。它的默认值是当前类名,且首字母小写
@Controller
@Controller
public class AccountController {
@Service
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Repository
@Repository("accountDao")
public class AccountDaoImpl1 implements AccountDao {
作用:@Service、@Controller、Repository他们的作用和属性与@Component 是一模一样的。
他们三个是Spring 框架为我们提供明确的三层使用的注解, 使我们三层对象更加清晰
@Autowired
@Autowired
private AccountDao accountDao;
作用:自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功。
- 如果Ioc容器中没有任何bean的类型和要注入的变量类型匹配,则报错。
- 如果Ioc容器中有多个类型匹配时:
- 出现位置:可以是变量上,也可以是方法上
- 细节:在使用注解时,set方法就不是必须的了
@Qualifier
@Autowired
@Qualifier("accountDao2")
private AccountDao accountDao;
- 作用:在按照类中注入的基础之上再按照名称注入。它在给类成员注入时不能单独使用,但是在给方法参数注入时可以。(不能独立使用)
- 属性:value :用于指定注入bean的id
@Resource
@Resource(name = "accountDao2")
private AccountDao accountDao;
- 作用:直接按照bean 的id注,它可以独立使用
- 属性:name :用于指定注入bean的id
@Scope
@Service("accountService")
@Scope("prototype")
public class AccountServiceImpl implements AccountService {
- 作用:用于指定bean 的作用范围
- 属性:value:指定范围的取值。常用取值:singleton 、prototype
@PostConstruct
@PostConstruct
public void init(){
System.out.println("初始化方法执行了");
}
作用:用于指定销毁方法
@Configuration
@Configuration
@PropertySource("classpath:jdbcConfig.properties")
public class JdbcConfig {
作用:指定当前类是一个配置类
@ComponentScan
@ComponentScan(basePackages = "cn.bloghut")
public class SpringConfiguration {
- 作用:用于通过注解指定spring 创建容器时要扫描的包
- 属性:value :它和 basePackages 的作用是一样的,都是用于指定创建容器时要扫描的包
@Bean
/**
* 创建数据库源对象
* @return
*/
@Bean(name = "dataSource")
public DataSource createDataSource(){
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setUser(username);
ds.setPassword(password);
return ds;
}catch (Exception e){
throw new RuntimeException("初始化数据库失败");
}
}
- 作用:用于把当前方法的返回值作为bean 对象存入spring 的ioc容器中
- 属性:name:用于指定bean的id。当不写时,默认值是当前方法的名称
- 细节:但我们使用注解配置方法时,如果方法有参数,spring 框架回去容器中查找有没有可用的 bean 对象,查找方式和Autowired注解的作用一样。
@Import
@Import(JdbcConfig.class)
public class SpringConfiguration {
作用:用于导入其他的配置类
@Aspect
@Component("logger")
public class Logger {
作用:表示当前类是一个切面类
@Pointcut
@Pointcut("execution(* cn.bloghut.service.impl.*.*(..))")
private void pt1(){}
作用:配置切入点表达式
@Before
@Before("pt1()")
public void beforePrintLog(){
System.out.println("Logger类的beforePrintLog()方法开始记录日志了");
}
作用:前置通知
@AfterReturning
@AfterReturning("pt1()")
public void afterReturningPrintLog(){
System.out.println("Logger类的afterReturningPrintLog()方法开始记录日志了");
}
作用:后置通知
@AfterThrowing
@AfterThrowing
public void afterThrowingPrintLog(){
System.out.println("Logger类的beforeThrowingPrintLog()方法开始记录日志了");
}
作用:异常通知
@AfterThrowing
@After("pt1()")
public void afterPrintLog(){
System.out.println("Logger类的afterPrintLog()方法开始记录日志了");
}
作用:最终通知
@Around
@Around("pt1()")
public Object aroundPrintLog(ProceedingJoinPoint proceedingJoinPoint){
Object resultValue = null;
//明确调用业务层方法(切入点方法)
try {
//得到方法执行所需的参数
Object[] args = proceedingJoinPoint.getArgs();
System.out.println("Logger类的aroundPrintLog()方法开始记录日志了---前置");
resultValue = proceedingJoinPoint.proceed();
System.out.println("Logger类的aroundPrintLog()方法开始记录日志了---后置");
return resultValue;
}catch (Throwable throwable){
System.out.println("Logger类的aroundPrintLog()方法开始记录日志了---异常");
throw new RuntimeException(throwable);
}finally{
System.out.println("Logger类的aroundPrintLog()方法开始记录日志了---最终");
}
}
作用:环绕通知
@Transactional
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class AccountServiceImpl implements AccountService {
作用:只读型事务的配置