Spring4学习笔记-通过注解配置bean
来源:互联网 发布:80c51单片机引脚功能 编辑:程序博客网 时间:2024/05/16 00:29
通过注解配置Bean
TestObject.java
package com.spring.beans.annotation;import org.springframework.stereotype.Component;; @Componentpublic class TestObject { }UserController.java
package com.spring.beans.annotation.controller; import org.springframework.stereotype.Controller; @Controllerpublic 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; @Servicepublic 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); }}
0 0
- Spring4学习笔记-通过注解配置bean
- Spring4学习笔记(八):通过注解的方式配置bean和@Autowired 自动装配
- Spring4学习(三):注解配置bean
- Spring学习笔记三: 通过注解配置Bean
- spring学习笔记----通过注解配置Bean(1)
- spring学习笔记----通过注解配置Bean(2)
- Spring学习笔记之通过注解配置Bean
- Spring学习笔记之通过注解配置Bean(1)
- Spring学习笔记之通过注解配置Bean(2)
- Spring4.0学习笔记006——Bean的配置(基于注解)
- spring学习十一 通过注解配置bean
- 通过注解配置Bean
- spring4+mybatis 配置注解装载bean
- Spring学习笔记----注解方式配置bean
- 13.Spring学习笔记_通过注解配置Bean(by尚硅谷_佟刚)
- Spring4 IOC 注解学习笔记
- Spring4----使用注解方式配置bean和bean的属性
- spring通过注解配置Bean
- struts2返回JSON配置
- struts线程安全
- STL源码解析1—六大组件关系
- Cocos2d-x教程(5)-播放音乐与音效
- 用户级线程和内核级线程
- Spring4学习笔记-通过注解配置bean
- Git资料---在win7系统下使用TortoiseGit(乌龟git)简单操作Git@OSC
- 面向对象的三个基本特征(讲解)
- MediaPlayerError (1,-110)的问题
- 【LeetCode】-Best Time to Buy and Sell Stock II
- 你对我的信任,是我自己毁掉的
- 数据库命名规范--通用
- js中Map之基本用法分开单独写方法,当做类库使用
- 谈内存对齐