基于注解方法实现bean管理
什么是注解
注解就是代码特殊标记,格式:@注解名称(属性名称=属性值...)
使用注解,注解可以作用在类,方法,属性等
使用注解的目的
为了简化xml配置
Spring针对Bean管理中创建对象提供了四个注解
@Component
@Service
@Controller
@Repository
以上四个注解功能是一样的,都可以用来创建bean实例
基于注解的方式实现对象的创建
第一步 引入依赖
<?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>org.example</groupId> <artifactId>c</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.14</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.14</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.14</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.3.14</version> </dependency> <!-- 日志包 --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.15</version> </dependency> </dependencies> </project>
第二步 开启组件扫描
<?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="com.test4.dao,com.test4.service"/>--> <context:component-scan base-package="com.test4"/> </beans>
第三步 添加注解
package com.test4.service; import org.springframework.stereotype.Component; //在注解里面value属性值可以省略不写 //默认值是雷名称,首字母小写 @Component(value = "userService") public class UserService { public void add(){ System.out.println("userService add"); } }
第四步 测试
package com.test4.service; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import static org.junit.Assert.*; public class UserServiceTest { @Test public void add() { ApplicationContext context = new ClassPathXmlApplicationContext("spring12.xml"); UserService userService = context.getBean("userService", UserService.class); System.out.println("userService = " + userService); userService.add(); } }
userService = com.test4.service.UserService@7ea37dbf
userService add
开启组件扫描配置
<?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="com.test4.dao,com.test4.service"/>--> <context:component-scan base-package="com.test4"/> <!-- 开启组件扫描配置 示例1 use-default-filters="false" 表示现在不使用默认filter,使用自己配置的filter context:include-filter 扫描那些内容 --> <context:component-scan base-package="com.test4" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 开启组件扫描配置 示例2 context:exclude-filter 不去扫描那些内容 --> <context:component-scan base-package="com.test4"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>