1.注解方式创建IOC
@Component 放在类上,用于标记,告诉spring当前类需要由容器实例化bean并放入容器中
该注解有三个子注解:本质上是一样的,只是用于区分不同层的
@Controller 用于实例化controller层bean
@Service 用于实例化service层bean
@Repository 用于实例化持久层bean
【1】创建Maven项目,pom.xml导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
【2】bean包下创建两个对象,User对象和Student对象
public class User {
}
public class Student {
}
【3】applicationContext.xml添加注解扫描
添加注解扫描,扫描指定的包,将包中的所有有注解的类实例化
base-package 后面放要扫描的包
如果有多个包需要扫描,可以使用逗号隔开 com.jlx.bean,com.jlx.service
或者可以写上一层包路径 com.jlx
可以通过注解指定bean的id@Component("user1")
如果不指定,则id默认是 类名首字母小写
<context:component-scan base-package="com.msb.bean"></context:component-scan>
【4】在User对象上面添加注解,测试
@Component("user1")
public class User {
}
@Test
public void testGetBean(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user1", User.class);
System.out.println(user);
}
【5】测试结果
注意:如果User对象的注解是Controller注解,Student对象使用的是Service,现在我们只想扫描Controller注解,需要更改XML文件、
use-default-filters="true" 使用默认的扫描过滤器
默认的扫描过滤器会识别并包含 @Component @Controller @Service @Repository 四个注解
include-filter 控制只扫描哪些IOC注解
exclude-filter 控制不扫描哪些IOC注解
<context:component-scan base-package="com.jlx.bean" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
2.注解方式依赖注入
2.1用@Autowire配合 @Qualifier注入指定对象
【1】创建dao层,然后创建UserDao接口并实现这个接口,这个接口有两个实现类,UserDaoImplA和UserDaoImplB。
public interface UserDao {
void add();
}
@Repository
public class UserDaoImplA implements UserDao {
@Override
public void add() {
System.out.println("UserDaoImplA add ... ...");
}
}
@Repository
public class UserDaoImplB implements UserDao {
@Override
public void add() {
System.out.println("UserDaoImplB add ... ...");
}
}
【2】创建service层,然后创建UserService接口并实现这个接口。
public interface UserService {
void add();
}
package com.jlx.service.impl;
import com.jlx.dao.UserDao;
import com.jlx.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@Service
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier("userDaoImplA")
//@Qualifier("userDaoImplB")
//private UserDao userDao;
@Override
public void add() {
System.out.println("UserServiceImpl add ... ...");
userDao.add();
}
}
@Autowired:根据类型到容器中去寻找对应的对象,找到后给当前属性赋值,不需要依赖 set方法,属性类型可以是接口,会自动匹配对应的实现类对象。
@Autowired配合 @Qualifier,可以通过名称指定注入的对象。
【3】XML文件配置
<context:component-scan base-package="com.jlx"></context:component-scan>
【4】测试:
@Test
public void testGetBean(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext2.xml");
UserServiceImpl userService = context.getBean("userServiceImpl", UserServiceImpl.class);
userService.add();
}
【5】测试结果
注:一般情况下只会有一个实现类,我这里用两个只是为了配合一下@Qualifier,一般情况下不使用这个注解。
2.2用@Resources注入
更改UserDaoImpl实现类的内容,添加@Resources注解
package com.jlx.service.impl;
import com.jlx.dao.UserDao;
import com.jlx.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserServiceImpl implements UserService {
@Resource("userDaoImplA")
private UserDao userDao;
@Override
public void add() {
System.out.println("UserServiceImpl add ... ...");
userDao.add();
}
}
注:可以看出,@Resource是jdk的一个注解,Spring官方推荐使用@Autowire这个注解,这样可以降低Spring和jdk的一个耦合度。
2.3普通数据类型的属性赋值 (8种基本+String)
【1】创建一个aaa.properties文件,这里文件编码全部的是UTF-8
sname=xiaoji小白
sage=21
sgender=boy
【2】XML文件将文件内容加入容器
<context:property-placeholder location="classpath:aaa.properties"></context:property-placeholder>
<context:component-scan base-package="com.jlx"></context:component-scan>
【3】给属性赋值
@Value:给普通属性赋值,使用${}这种表达式获取系统的变量值
package com.jlx.service.impl;
import com.jlx.dao.UserDao;
import com.jlx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier("userDaoImplA")
// 普通数据类型的属性赋值 8+String
@Value("${sname}")
private String sname;
@Value("${sgender}")
private String sgender;
@Value("${sage}")
private Integer sage;
@Override
public void add() {
System.out.println("UserServiceImpl add ... ...");
System.out.println(sname);
System.out.println(sgender);
System.out.println(sage);
userDao.add();
}
}
【4】测试
@Test
public void testGetBean(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext2.xml");
UserServiceImpl userService = context.getBean("userServiceImpl", UserServiceImpl.class);
userService.add();
}
【5】测试结果
附加:如果我们不想扫描XML文件,我们可以创建一个配置类,加上下面两个注解,然后测试的时候new的是一个AnnotationConfigApplicationContext即可。
@ComponentScan(basePackages = "com.jlx") //包扫描
@PropertySource("classpath:aaa.properties") //扫描配置文件