package com.itcast.service.impl;
import com.itcast.dao.AccountDao;
import com.itcast.dao.impl.AccountDaoImpl;
import com.itcast.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.annotation.Resources;
import java.util.Date;
/**
* 账户业务层实现类
*
* 曾经xml的配置
* <bean id="accountService" class="com.itcast.service.impl.AccountServiceImpl"
* scope="" init-method="" destroy-method="">
* <property name="" value="" ref="" ></property>
* </bean>
*
* 用于创建对象的
* 作用就和在xml配置文件中编写一个<bean>标签实现的功能一样的
* Component
* 作用: 用于把当前类对象存入spring容器中
* 属性: value 指定bean的id, 不写时候 默认值时当前类名 首字母小写
* Controller 表现层
* Service 业务层
* Repository 持久层
* 以上三个注解的作用与属性与Component一样的,
* 这三个是为我们提供明确的三层使用的注解吗使我们三层对象更加清晰
* 用于注入数据的
* 作用就和在xml配置文件中的<bean>标签写一个<property>标签的作用是一样的
* Autowired:
* 作用 按照类型注入, 只要容器中有唯一的一个bean对象和要注入的类型匹配,就可以注入成功
* 如果ioc容器中没有任何bean的类型和要注入的变量类型匹配,则报错
* 如果ioc容器中有多个bean的类型和要注入的变量类型匹配,则报错
* 出现位置:
* 可以是变量上 也可以时方法上
* 在使用注解注入时 set方法就不是必须的了
* Qualifier:
* 按照类型注入的基础上再按照名称注入,给类成员注入时不能单独使用,给方法参数注入可以单独使用
* 属性:
* value: 用于指定注入bean的id
* Resource
* 作用 直接按照bean的id注入,可以独立使用
* 属性:
* name:用于指定注入bean的id
*
* 以上三个注解 只能注入其他bean类型的数据, 基本类型和String 无法使用上述注解实现.
* 集合类型注入只能通过XML来实现
*
* Value
* 作用 注入基本类型和String的数据
* 属性:
* value: 指定数据的值, 可以使用spring 中的SpEL(spring 的el表达式)
* SpEL的写法 ${表达式}
* 用于改变作用范围的
* 作用就和在<bean>标签中使用scope属性实现功能是一样的
* Scope
* 作用: 用于指定bean的作用范围
* 属性
* value指定范围的取值. singleton prototype
* 和声明周期相关的 (了解)
* 作用就和在<bean>标签中使用init-method和destroy-method属性是一样的
* PreDestroy 用于指定销毁方法
* PostConstruct 用于指定初始化方法
*/
@Component("accountService")
public class AccountServiceImpl implements AccountService {
// @Autowired
@Qualifier("accountDao1")
@Resource(name="accountDao2")
private AccountDao accountDao;
@PostConstruct
public void init(){
System.out.println("对象初始化............");
}
@PreDestroy
public void destroy(){
System.out.println("对象销毁.............");
}
public void saveAccount() {
System.out.println("Service 中的saveAccount方法执行.....");
accountDao.saveAccount();
}
}
/**
* 该类是一个配置类 作用和bean.xml是一样的
* spring 中的新注解
* Configuration
* 作用: 指定当前类是一个注解类
* 当配置类作为AnnotationConfigApplicationContext创建的参数时 可不写
* ComponentScan
* 用于通过注解指定spring在创建容器时 要扫描的包
* 属性:
* value 和basePackages的作用是一样的 指定创建容器时候 要扫描的包
* 使用此注解等于配置:
* <context:component-scan base-package="com.itcast"></context:component-scan>
* Bean
* 作用: 把当前方法的返回值 作为bean对象存入spring的IOC容器中
* 属性:
* name: 用于指定bean的id 默认值时当前方法的名称
*
*
* 当使用注解配置方法时,如果方法有参数 spring 会去容器中查找有没有可用的bean对象
* 查找方式和Autowired是一样的
*
* Import 导入其他配置类
* 属性:
* value 指定其他配置类的字节码
* 使用import类只后, 有Import注解的类 就是父配置类 导入的都是子配置类
*
* PropertySource
* 作用 用于指定properties文件的位置
* 属性:
* value 指定问卷的名称和路径
* 关键字classpath 表示类路径下
*/
//@Configuration
@ComponentScan("com.itcast")
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
}