1:@component:基本注解,标识了一个受Spring管理的主键
2:@respository:标识持久层主键
3:@Service:标识服务层(业务层)主键
4:@Controller:标识表示层主键
•Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称
使用注解,需要在Spring的配置文件中声明:<context:component-scan>
–base-package 属性指定一个需要扫描的基类包,Spring 容器将会扫描这个基类包里及其子包中的所有类.
–当需要扫描多个包时, 可以使用逗号分隔.
如果仅希望扫描特定的类而非基包下的所有类,可使用 resource-pattern 属性过滤特定的类
–<context:include-filter> 子节点表示要包含的目标类 需要在<context:component-scan>配置use-default-filters="false"
–<context:exclude-filter> 子节点表示要排除在外的目标类
–<context:component-scan> 下可以拥有若干个 <context:include-filter> 和 <context:exclude-filter> 子节点
<?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-4.0.xsd"> <!-- 指定Spring IOC容器扫描的包 --> <!-- resource-pattern 扫描指定的资源 --> <!-- <context:component-scan base-package="com.test.spring.annotation" resource-pattern="controller/*.class" ></context:component-scan>--> <!-- exclude-filter 子节点指定排除那些指定表达式的组件 --> <!-- include-filter 子节点包含那些表达式的组件,需要配置 use-default-filters为false--> <context:component-scan base-package="com.test.spring.annotation" use-default-filters="false"> <!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> --> <!-- - <context:exclude-filter type="assignable" expression="com.test.spring.annotation.TestObject"/> --> <context:include-filter type="assignable" expression="com.test.spring.annotation.TestObject"/> </context:component-scan> </beans>
package com.test.spring.annotation; import org.springframework.stereotype.Component; @Component public class TestObject { } package com.test.spring.annotation.controller; import org.springframework.stereotype.Controller; @Controller public class UserController { public void execute(){ System.out.println("usercontroller"); } } package com.test.spring.annotation.repository; public interface UserRepository { public void save(); } package com.test.spring.annotation.repository; import org.springframework.stereotype.Repository; //注解可以通过value属性值标识,bean在IOC容器中的name @Repository("userRepository") public class UserRepositoryImlpl implements UserRepository{ public void save() { System.out.println("实现接口的save"); } } package com.test.spring.annotation.service; import org.springframework.stereotype.Service; @Service public class UserService { public void add(){ System.out.println("USERSERVICE的 add"); } }
package com.test.spring.annotation; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.test.spring.annotation.controller.UserController; import com.test.spring.annotation.repository.UserRepository; import com.test.spring.annotation.repository.UserRepositoryImlpl; import com.test.spring.annotation.service.UserService; public class Main { public static void main(String[] args) { ApplicationContext con=new ClassPathXmlApplicationContext("beans_annocation.xml"); TestObject to=(TestObject) con.getBean("testObject"); System.out.println(to); UserService ser=(UserService) con.getBean("userService"); System.out.println(ser); UserController ucon=(UserController) con.getBean("userController"); System.out.println(ucon); UserRepositoryImlpl ur=(UserRepositoryImlpl) con.getBean("userRepository"); System.out.println(ur); } }
时间: 2024-10-26 02:49:17