Spring框架入门之基于Java注解配置bean

Spring框架入门之基于Java注解配置bean

一、Spring bean配置常用的注解

  常用的有四个注解

  • Controller: 用于控制器的注解
  • Service : 用于service的注解
  • Component: 用于基本组件的注解
  • Repository:用于Dao层的注解
  • 其实,对于spring来说,它根本无法识别controller,service等,它只知道只要你加了以上四个注解,它就帮你创建bean
  • 简单来说,就是如果你在控制器上使用Component注解,或者使用Repository注解也是可以的,四种注解可以混用
  • 但是,我们一般都按照上方所示的规则来使用注解,这样代码才有可读性

二、使用注解

要说的都在代码注释中,直接上代码:

包结构如下:

Spring框架入门之基于Java注解配置bean

配置文件如下: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包下的类

 package me.spring.beans.annotation;

 import org.springframework.stereotype.Component;

 @Component
public class TestObject { }
package me.spring.beans.generic.di; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-generic-di.xml");
UserService userService = (UserService) ctx.getBean("userService");
userService.add();
}
}

me.spring.beans.annotation.controller包下的类:

 package me.spring.beans.annotation.controller;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import me.spring.beans.annotation.service.UserService; /**
* 模拟表现层
* @author Administrator
*
*/
@Controller
public class UserController { @Autowired
private UserService userService;
public void execute() {
System.out.println("UserController's execute method");
userService.add();
}
}

me.spring.beans.repository包下的类

 package me.spring.beans.annotation.repository;

 import org.springframework.stereotype.Repository;

 @Repository
public class UserJdbcRepository implements UserRepository { @Override
public void save() { System.out.println("UserJdbcRepository's save");
} } package me.spring.beans.annotation.repository; public interface UserRepository { public void save();
} package me.spring.beans.annotation.repository; import org.springframework.stereotype.Repository; /**
* 模拟持久化层
* @author Administrator
*
*/
@Repository
public class UserRepositoryImpl implements UserRepository{ @Override
public void save() { System.out.println("UserRepository's save method");
} }

me.spring.beans.service包下的类

 package me.spring.beans.annotation.service;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service; import me.spring.beans.annotation.repository.UserRepository; /**
* 模拟业务层
* @author Administrator
*
*/
@Service
public class UserService { @Autowired
@Qualifier("userJdbcRepository")
private UserRepository userRepository;
public void add() {
System.out.println("UserService's add method");
userRepository.save();
}
}

参考:http://blog.csdn.net/u010837612/article/details/45577817

上一篇:BZOJ 2006: [NOI2010]超级钢琴 [ST表+堆 | 主席树]


下一篇:MyEclipse 10.7.1 最新版官网下载地址