<bean id="accountService" class="org.example.service.AccountService"
scope="singleton" init-method="" depends-method="">
<property name="strs">
<array>
<value>值1</value>
<value>值2</value>
</array>
</property>
需要aop的jar包
使用注解前需要先需要添加配置,创建容器时扫描需要被管理类所在的包
<?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">
<!-- 创建容器时要扫描的包-->
<context:component-scan base-package="org.example"></context:component-scan>
</beans>
用于创建对象的注解
和XML的bean标签一致
@Component : 把当前类对象存入spring容器中
属性:value:用于指定bean的ID,如果不写,默认类名首字母小写。
以下三个注解和Component 功能一致,为了明确MVC三层架构可以使用以下三个注解
@Controller 一般用在表现层
@Service 一般用在业务层
@Repository 一般用在持久层
import org.springframework.stereotype.Component;
@Component
public class AccountService {
public void run() {
System.out.println("对象创建使用成功。。。");
}
}
import org.example.dao.AccountDao;
import org.example.service.AccountService;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Main {
public static void main(String[] args) {
//获取对象配置文件
Resource resource = new ClassPathResource("springBean.xml");
//加载对象配置文件
BeanFactory factory = new XmlBeanFactory(resource);
//根据id获取bean对象
AccountService accountService = (AccountService) factory.getBean("accountService");
//对象调用测试
accountService.run();
}
}
用于注入数据的注解
注入bean类型数据的注解
和bean标签的子标签<property>一致
@Autowired 自动按照类型注入
位置:可以是变量上,也可以是方法上。
当以接口类注入对象时,如果接口类有多个实现类,需要明确注入的对象,如果没有注明或者接口没有实现类会导致注入失败。
代码示例:
1.创建一个接口
public interface AccountDao {
void run();
}
2.给接口创建两个实现类
@Repository("accountDao1")
public class AccountDaoImpl implements AccountDao {
public void run() {
System.out.println("我是查数据库的。。。。");
}
}
@Repository("accountDao2")
public class AccountDaoImpl2 implements AccountDao {
public void run() {
System.out.println("我是查数据库的。。。。");
}
}
3.注入调用测试
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao2 ;//多个实现类调用,通过接口类创建注入对象数据时,变量名需要和创建bean的ID一致,否者会导致注入失败
public void run() {
accountDao2.run();
}
}
@Qualifier
作用:在按照类中注入的基础上再按照名称注入,他在给类成员注入时需要和@Autowired 一起使用指定注入的bean的ID,在给方法参数注入时可以
属性:value 用于指定注入bean的ID,默认属性可省略
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
@Qualifier("accountDao2")
private AccountDao accountDao;
public void run() {
accountDao.run();
}
}
@Resource (java自带注解,非spring的注解)
作用:可以直接指定的bean 的ID,不需要Autowired 配合使用。
import javax.annotation.Resource;
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Resource(name = "accountDao2")
private AccountDao accountDao;
public void run() {
accountDao.run();
}
}
注入基本类型和String类型的注解
@Autowired @Qualifier @Resource 都是注入bean类型的数据,集合只能使用XML注入
注解如下:
@value:用于指定数据的值,可以使用spring的EL表达式 ${.....}
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Resource(name = "accountDao2")
private AccountDao accountDao;
@Value("哈哈")
private String str;
@Value("1")
private int i;
public void run() {
System.out.println("str="+str + ";+i="+i);
accountDao.run();
}
}
用于改变作用范围(生命周期)的注解
和bean标签的子标签的scope属性一致
注解: @Scope
作用:指定bean的作用范围。
取值:single单例(默认),prototype多例
@Repository
//@Scope(value = "single")
@Scope(value = "prototype")
public class AccountDaoImpl implements AccountDao {
public void run() {
System.out.println("我是查数据库的。。。。");
}
}