XML配置
Spring中的依赖注入
构造函数注入
public class AccountServiceImpl implements AccountService {
/*经常变化的数据,不适合注入的方式*/
private String name;
private Integer age;
private Date time;
public AccountServiceImpl(String name, Integer age, Date time) {
this.name = name;
this.age = age;
this.time = time;
}
@Override
public void saveAccount() {
System.out.println("service中的方法执行了!"+name+","+age+","+time);
}
}
bean.xml
<bean id="accountService" class="com.lg.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="校历"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="time" ref="now"></constructor-arg>
</bean>
<!--配置一个日期对象-->
<bean id="now" class="java.util.Date"/>
set方法注入
AccountServiceImpl1.java
public class AccountServiceImpl1 implements AccountService {
/*经常变化的数据,不适合注入的方式*/
private String name;
private Integer age;
private Date time;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setTime(Date time) {
this.time = time;
}
@Override
public void saveAccount() {
System.out.println("service中的方法执行了!"+name+","+age+","+time);
}
}
<!--配置一个日期对象-->
<bean id="now" class="java.util.Date"/>
<bean id="accountService1" class="com.lg.service.impl.AccountServiceImpl1">
<property name="name" value="two"/>
<property name="age" value="20"/>
<property name="time" ref="now"/>
</bean>
复杂类型注入注入
public class AccountServiceImpl2 implements AccountService {
private String[] myArray;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties properties;
public void setMyArray(String[] myArray) {
this.myArray = myArray;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
@Override
public void saveAccount() {
System.out.println(Arrays.toString(myArray));
System.out.println(myList);
System.out.println(mySet);
System.out.println(myMap);
System.out.println(properties);
}
}
<!--复杂类型的注入-->
<bean id="accountService2" class="com.lg.service.impl.AccountServiceImpl2">
<property name="myArray">
<array>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</array>
</property>
<property name="myList">
<list>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</list>
</property>
<property name="mySet">
<set>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</set>
</property>
<property name="myMap">
<map>
<entry key="testA" value="AAA"></entry>
<entry key="testB" value="BBB"></entry>
<entry key="testC" value="CCC"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="1">111</prop>
<prop key="2">222</prop>
<prop key="3">333</prop>
</props>
</property>
</bean>
注解配置
告知Spring在创建容器时要扫描的包
<?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.lg"/>
</beans>
用于创建对象的
@Component("accountService")
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao = new AccountDaoImpl();
public AccountServiceImpl(){
System.out.println("创建对象!");
}
@Override
public void saveAccount() {
accountDao.save();
}
}
用于注入数据的
按照类型注入域属性值@Avtowired
@Component("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao1 ;
public AccountServiceImpl(){
System.out.println("创建对象!");
}
@Override
public void saveAccount() {
accountDao1.save();
}
}
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
@Override
public void save() {
System.out.println("保存账户000000!");
}
}
@Repository("accountDao1")
public class AccountDaoImpl1 implements AccountDao {
@Override
public void save() {
System.out.println("保存账户1111111!");
}
}
按照名称注入域属性值@Avtowired与@Qualifier
@Component("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
@Qualifier("accountDao")
private AccountDao accountDao1 ;
public AccountServiceImpl(){
System.out.println("创建对象!");
}
@Override
public void saveAccount() {
accountDao1.save();
}
}
域属性注解@Resource
@Component("accountService")
public class AccountServiceImpl implements AccountService {
@Resource(name = "accountDao")
private AccountDao accountDao1 ;
public AccountServiceImpl(){
System.out.println("创建对象!");
}
@Override
public void saveAccount() {
accountDao1.save();
}
}
以上三个注入只能注入其他Bean类型的数据,而基本类型和String类型无法使用上述注解实现。另外,集合类型的注入只能通过XML来实现。
基本类型属性注入@Value
用于改变作用范围的@Scope
和生命周期相关
作用在Bean标签中使用init-method
和destory-method
作用相同。
@PreDestroy
:用于指定销毁方法
@PostConstruct
:用于指定初始化方法
@Component("accountService")
public class AccountServiceImpl implements AccountService {
@PreDestroy
public void destoeyMethod(){
System.out.println("方法销毁!");
}
@PostConstruct
public void initMethod(){
System.out.println("方法初始化!");
}
@Resource(name = "accountDao")
private AccountDao accountDao1 ;
public AccountServiceImpl(){
System.out.println("创建对象!");
}
@Override
public void saveAccount() {
accountDao1.save();
}
}
public static void main(String[] args) {
//1、获取核心容器对象
//ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//根据beanId获取对象
//AccountService as = (AccountService) ac.getBean("accountService");
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
AccountService as = (AccountService) ac.getBean("accountService");
as.saveAccount();
ac.close();
}
其他注解
/**
* 该类是一个配置类,和xml类似
* 新注解
* Configuration
* 指定当前类是一个配置类
* ComponentScan
* 使用注解指定1Spring在创建容器时要扫描的包
* 属性:value 和basePackages相同,都是用于指定创建容器时要扫描的包/使用
* 此注解就相当于配置xml了。
* <context:component-scan base-package="com.lg"></context:component-scan>
* Bean
* 作用:用于把当前方法的返回值作为Bean对象存入Spring的IOC容器中
* 属性:name用于指定Bean的Id,当不写时,默认值是当前方法的名称
* 细节:当我们使用注解配置方式时,如果方法有参数,Spring框架会去容器中查找有没有可用的Bean对象,
* 查找方式和Autowired注解的作用是一样的。
*
*/
@Configuration
@ComponentScan(basePackages = "com.lg")
public class SpringConfiguration {
/**
* 用于创建一个QueryRunner对象
* @param dataSource
* @return
*/
@Bean(name = "runner")
public QueryRunner createQueryRuner(DataSource dataSource){
return new QueryRunner(dataSource);
}
@Bean("dataSource")
public DataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.cj.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/eesy?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC");
ds.setUser("root");
ds.setPassword("123456");
return ds;
}
}