学习基于注解的 IoC 配置,大家脑海里首先得有一个认知,即注解配置和 xml 配置要实现的功能都是一样
的,都是要降低程序间的耦合。只是配置的形式不一样。 在这里便不使用xml,只使用注解的形式来完成IoC的配置
1. 创建一个Maven Project,并在pom.xml中配置spring框架和对于的测试的jar包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringTest02</groupId>
<artifactId>day02_eesy_02account_xmlioc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
2. 在项目的src目录下,创建一个Annotations包,在该包中创建子包dao,并在包中创建接口IUserDao,以及对应的接口实现类UserDaoImpl
public interface IUserDao {
void say();
}
其中@Repositor表示对其属性赋值 @Controller @Service @Repository
他们三个注解都是针对一个的衍生注解,他们的作用及属性都是一模一样的。 他们只不过是提供了更加明确的语义化。
@Controller:一般用于表现层的注解。 @Service:一般用于业务层的注解。
@Repository:一般用于持久层的注解。 细节:如果注解中有且只有一个属性要赋值时,且名称是
value,value在赋值是可以不写。
@Repository("userDao")
public class UserDaoImpl implements IUserDao {
public void say() {
System.out.println("userdao.....say....");
}
}
3. 在Annotations.service包下在创建接口IUserService,以及对应的接口实现类UserServiceImpl
public interface IUserService {
void say();
}
@Autowired 作用: 自动按照类型注入。当使用注解注入属性时,set方法可以省略。它只能注入其他 bean 类型。 当有多个
类型匹配时,使用要注入的对象变量名称作为 bean 的 id,在 spring 容器查找,找到了也可以注入成功。 找不到 就报错。
@Service("userService")
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao;
public void say() {
this.userDao.say();
System.out.println("userService.....say");
}
}
4. 由于不使用xml来进行配置,因此要自己创建一个配置类来。再单独创建一个config包,在该包下创建SpringConfiguration.java,注:该类是一个配置类,它的作用和bean.xml是一样的
onfiguration 作用:指定当前类是一个配置类
细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数是,该注解可以不写.
ComponentScan 作用:用于通过注解指定Spring在创建容器时要扫描的包 属性:
value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。 我们使用该注解就等同于在xml中配置了:
<context:component-scan
base-package="com.itheima’></context:component-scan>
@Configuration
@ComponentScan("Annotations")
public class SpringConfiguration {
}
5. 在test.java中创建测试类nnotationAssembleTest.java
@ContextConfiguration 注解: locations 属性:用于指定配置文件的位置。如果是类路径下,需要用 classpath:表明 classes
属性:用于指定注解的类。当不使用 xml 配置时,需要用此属性指定注解类的位置。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AnnotationAssembleTest {
@Autowired
private IUserService service;
@Test
public void TestAnnotation()
{
service.say();
}
}
6. 输出结果如下
总结:虽然使用纯注解的方式看来确实是配置简单,维护方便(我们找到类,就相当于找到了对应的配置)。 但始终过于复杂,在修改时还要修改源码中对应的注解,这就会导致要是项目过大会导致修改时过于麻烦,因此我认为对应IoC的配置应该要将xml和注解结合着使用比较方便。