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包下的类

 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

Spring框架入门之基于Java注解配置bean的更多相关文章

  1. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  2. IDEA+Tomcat+Maven+SpringMVC基于Java注解配置web工程

    1.在IDEA中新建Maven工程,使用archetype. 2.添加Maven依赖 <dependencies> <dependency> <groupId>ju ...

  3. Spring框架学习笔记(3)——配置bean

    1.属性注入 (1)根据setter方法属性注入,这里使用的是property标签.需要bean属性提供对应的setter方法,比如笔记(1)里的 HelloWorld使用的就是这种方法. <! ...

  4. Spring Security入门(基于SSM环境配置)

    一.前期准备 配置SSM环境 二.不使用数据库进行权限控制 配置好SSM环境以后,配置SpringSecurity环境 添加security依赖   <dependency> <gr ...

  5. Spring框架学习笔记(4)——配置bean more

    1.配置List属性 <!-- 配置List属性 --> <bean id="person4" class="com.broadtext.beans.c ...

  6. Spring基于Java的配置

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html: 基于Java的配置选项,可以使你在不用 ...

  7. Spring入门学习笔记(2)——基于Java的配置

    目录 基于Java的配置 @Configuration & @Bean Annotations Example 注入Bean依赖 @Import注解 Lifecycle Callbacks(声 ...

  8. java spring是元编程框架---使用的机制是注解+配置

    java spring是元编程框架---使用的机制是注解+配置

  9. [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

随机推荐

  1. python课堂整理8---字符串格式化

    一.字符串格式化(% 和 format) 1. % s  主要接收字符串类型,也可以接收任意类型 tp1 = "i am %s my hobby is alex" % 'lhf' ...

  2. <<Modern CMake>> 翻译 2.2 CMake 编程

    <<Modern CMake>> 翻译 2.2 CMake 编程 流程控制 CMake有一个 if 语句, 经年累月之后,现在它已经相当复杂. 您可以在 if 语句中使用全大写 ...

  3. Java核心技术(卷一)读书笔记——第一章(概述)

    1.Java不提供多重继承,通过接口来实现.一个类只能继承一个父类,但是可以同时实现多个接口. 2.Java中的int类型的大小是固定的32位,以避免代码移植时候的不兼容问题.唯一的限制是int类型的 ...

  4. Android Studio "cannot resolve symbol R" 问题

    初接触Android Studio,又遇到了 "cannot resolve symbol R"问题(以前在 Eclipse 也遇到过),网上方法不一,后来在stackoverfl ...

  5. CentOS7使用yum安装ceph rpm包

    1. 安装centos7对扩展repo的支持yum install yum-plugin-priorities保证下面的选项是开启的[main]enabled = 1 2. 安装 release.ke ...

  6. ContentProvider 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  7. http客户端-性能比较系列-第一篇-单线程

    系列文章: 单线程性能测试:https://www.cnblogs.com/victor2302/p/11077208.html 多线程性能测试:https://www.cnblogs.com/vic ...

  8. Linux下SVN库迁移

    在日常的工作中,可能因为一些服务器硬件损坏等问题,不得不把SVN服务器上的SVN版本库进行迁移,下面讲解一下SVN库迁移方案(采用dump & load方案),在实际操作的时候也非常的简单,有 ...

  9. 基于SMS短信平台给手机发送短信

    JAVA发送手机短信,我知道的有三种方式,恰逢项目需求,自己整理了基于SMS的短信发送,其他两种这里就说说一下 使用webservice接口发送手机短信,这个可以使用sina提供的webservice ...

  10. AOSP 预置 APP

    Android 系统预置 APP 是做 Framework 应用开发经常经常会遇到的工作,预置 APP 分为两种,一种是直接预置 APK,一种是预置带有源码的 APP. 预置 apk 示例说明 以 . ...