码迷,mamicode.com
首页 > 编程语言 > 详细

Spring4学习笔记-通过注解配置bean

时间:2014-09-22 20:43:23      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:interface   package   public   接口   import   

通过注解配置Bean

bubuko.com,布布扣

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

标签:interface   package   public   接口   import   

原文地址:http://shamrock.blog.51cto.com/2079212/1557003

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!