首页 > 代码库 > Spring4学习笔记-通过注解配置bean
Spring4学习笔记-通过注解配置bean
通过注解配置Bean
TestObject.java
package com.spring.beans.annotation; import org.springframework.stereotype.Component;; @Component public class TestObject { }
UserController.java
package com.spring.beans.annotation.controller; import org.springframework.stereotype.Controller; @Controller public class UserController { public void execute() { System.out.println("UserController execute..."); } }
UserRepository.java接口
package com.spring.beans.annotation.repository; public interface UserRepository { void save(); }
UserRepositoryImpl.java
package com.spring.beans.annotation.repository; import org.springframework.stereotype.Repository; @Repository("userRepository") public class UserRepositoryImpl implements UserRepository{ @Override public void save() { // TODO Auto-generated method stub System.out.println("UserRepositoryImpl save..."); } }
UserService.java
package com.spring.beans.annotation.service; import org.springframework.stereotype.Service; @Service public class UserService { public void add() { System.out.println("UserService add..."); } }
beans-annotation.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 指定spring IOC容器扫描的包 --> <!-- 通过resource-pattern指定扫描的资源 --> <!-- 只扫描repository下的类 <context:component-scan base-package="com.spring.beans.annotation" resource-pattern="repository/*.class"></context:component-scan> --> <!-- 不扫描repository注解的类 。可以配置多个。 <context:component-scan base-package="com.spring.beans.annotation" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> --> <!-- 只扫描repository注解的类。可以配置多个。 <context:component-scan base-package="com.spring.beans.annotation" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> --> <!-- 不扫描某个接口。可以配置多个。使用assignable类型 <context:component-scan base-package="com.spring.beans.annotation" use-default-filters="true"> <context:exclude-filter type="assignable" expression="com.spring.beans.annotation.repository.UserRepository"/> </context:component-scan> --> <!-- 只扫描某个接口。 <context:component-scan base-package="com.spring.beans.annotation" use-default-filters="false"> <context:include-filter type="assignable" expression="com.spring.beans.annotation.repository.UserRepository"/> </context:component-scan> --> <context:component-scan base-package="com.spring.beans.annotation" ></context:component-scan> </beans>
Main.java
package com.spring.beans.annotation; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.beans.annotation.controller.UserController; import com.spring.beans.annotation.repository.UserRepository; import com.spring.beans.annotation.service.UserService; public class Main { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans-annotation.xml"); TestObject testObject = (TestObject) applicationContext.getBean("testObject"); System.out.println(testObject); UserController userController = (UserController) applicationContext.getBean("userController"); System.out.println(userController); UserService userService = (UserService) applicationContext.getBean("userService"); System.out.println(userService); UserRepository userRepository = (UserRepository) applicationContext.getBean("userRepository"); System.out.println(userRepository); } }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1557003
Spring4学习笔记-通过注解配置bean
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。