Spring框架入门之基于Java注解配置bean
Spring框架入门之基于Java注解配置bean
一、Spring bean配置常用的注解
常用的有四个注解
- Controller: 用于控制器的注解
- Service : 用于service的注解
- Component: 用于基本组件的注解
- Repository:用于Dao层的注解
- 其实,对于spring来说,它根本无法识别controller,service等,它只知道只要你加了以上四个注解,它就帮你创建bean
- 简单来说,就是如果你在控制器上使用Component注解,或者使用Repository注解也是可以的,四种注解可以混用
- 但是,我们一般都按照上方所示的规则来使用注解,这样代码才有可读性
二、使用注解
要说的都在代码注释中,直接上代码:
包结构如下:
配置文件如下: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: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.1.xsd">
<!--
指定SpringIOC容器扫描的包
可以使用resource-pattern指定扫描的资源
-->
<!--
<context:component-scan base-package="me.spring.beans.annotation" resource-pattern="repository/*.class"></context:component-scan>
-->
<!--
<context:component-scan
base-package="me.spring.beans.annotation"
resource-pattern="repository/*.class"
use-default-filters="false">
-->
<!-- context:exclude-filter子节点指定要排除的组件
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
-->
<!-- include-filter指定包含直接点的组件,需要use-default-filters配合使用
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
-->
<!--
<context:exclude-filter type="assignable" expression="org.springframework.stereotype.Repository"/>
<context:include-filter type="assignable" expression="org.springframework.stereotype.Repository"/>
-->
<!--
</context:component-scan>
-->
<context:component-scan base-package="me.spring.beans.annotation"></context:component-scan>
</beans>
me.spring.beans.annotation包下的类
1 package me.spring.beans.annotation;
2
3 import org.springframework.stereotype.Component;
4
5 @Component
6 public class TestObject {
7
8 }
9 package me.spring.beans.generic.di;
10
11 import org.springframework.context.ApplicationContext;
12 import org.springframework.context.support.ClassPathXmlApplicationContext;
13
14 public class Main {
15
16 public static void main(String[] args) {
17 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-generic-di.xml");
18 UserService userService = (UserService) ctx.getBean("userService");
19 userService.add();
20 }
21 }
me.spring.beans.annotation.controller包下的类:
1 package me.spring.beans.annotation.controller;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.stereotype.Controller;
5
6 import me.spring.beans.annotation.service.UserService;
7
8 /**
9 * 模拟表现层
10 * @author Administrator
11 *
12 */
13 @Controller
14 public class UserController {
15
16 @Autowired
17 private UserService userService;
18 public void execute() {
19 System.out.println("UserController's execute method");
20 userService.add();
21 }
22 }
me.spring.beans.repository包下的类
1 package me.spring.beans.annotation.repository;
2
3 import org.springframework.stereotype.Repository;
4
5 @Repository
6 public class UserJdbcRepository implements UserRepository {
7
8 @Override
9 public void save() {
10
11 System.out.println("UserJdbcRepository's save");
12 }
13
14 }
15
16 package me.spring.beans.annotation.repository;
17
18 public interface UserRepository {
19
20 public void save();
21 }
22
23 package me.spring.beans.annotation.repository;
24
25 import org.springframework.stereotype.Repository;
26
27 /**
28 * 模拟持久化层
29 * @author Administrator
30 *
31 */
32 @Repository
33 public class UserRepositoryImpl implements UserRepository{
34
35 @Override
36 public void save() {
37
38 System.out.println("UserRepository's save method");
39 }
40
41 }
me.spring.beans.service包下的类
1 package me.spring.beans.annotation.service;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.beans.factory.annotation.Qualifier;
5 import org.springframework.stereotype.Service;
6
7 import me.spring.beans.annotation.repository.UserRepository;
8
9 /**
10 * 模拟业务层
11 * @author Administrator
12 *
13 */
14 @Service
15 public class UserService {
16
17 @Autowired
18 @Qualifier("userJdbcRepository")
19 private UserRepository userRepository;
20 public void add() {
21 System.out.println("UserService's add method");
22 userRepository.save();
23 }
24 }
参考:http://blog.csdn.net/u010837612/article/details/45577817