spring 注解实例
先不说网上的那些例子了,百度到的都是一些零碎的东西。我之所以记博客,除了总结之外,很大一个原因是对网上的某些东西真的很无语。
拿注解来说,什么入门实例的东西,说是入门,却连一个基本的hello world 都没有,呵呵。
之前一直都是用xml配置,注解现在用的也多了,要好好看看。
本篇里面都是基础,代码清单都会列全。
首先是引入spring包,这里用的是maven,pom.xml加入:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
然后maven install,引入包。
接着,xml的配置文件,这里包括头文件,以及注解需要的配置:
beans.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-3.0.xsd"> <context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.spring.ioc"></context:component-scan>
</beans>
好了,从现在开始。
代码结构:

Man包下是第二个例子。
先说第一个例子,无接口的。
person.java:
package com.spring.ioc; import org.springframework.stereotype.Component; @Component
public class Person {
private String name;
private String sex; public Person() {
name="wang";
sex="man";
}
/* public Person(String name, String sex) {
super();
name="wang";
sex="man";
}*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
} }
里面初始化了一些数据,作为一个bean。
depart.java:
package com.spring.ioc; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class Depart { @Autowired
private Person person; public String getDepart(){
String s=person.getName()+" in depart";
return s;
}
}
这个是为了演示,在depart里面注入person。
主类测试用的:
package com.spring.ioc; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
Depart depart=(Depart) applicationContext.getBean("depart");
System.out.println(depart.getDepart());
}
}
运行后,结果:
wang in depart
第二个例子,带有接口的例子:
创建接口,man:
package com.spring.ioc.Man;
public interface Man {
public String say();
}
然后有两个实现类:
package com.spring.ioc.Man; import org.springframework.stereotype.Component; @Component
public class Chinese implements Man { public String say() {
// TODO Auto-generated method stub
return "你好";
} }
package com.spring.ioc.Man; import org.springframework.stereotype.Component; @Component
public class American implements Man { public String say() {
// TODO Auto-generated method stub
return "hello";
} }
然后创建一个类,注入这两个接口实现类。
package com.spring.ioc.Man; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; @Component
public class ManService {
@Autowired
@Qualifier(value="chinese")
private Man man; public String sayChineseHello(){
return man.say()+",欢迎";
}
@Autowired
@Qualifier(value="american")
private Man aman;
public String sayEnglishHello(){
return aman.say()+",welcome";
}
}
主类:
package com.spring.ioc.Man; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
ManService manService=(ManService) context.getBean("manService");
String string=manService.sayChineseHello();
System.out.println(string);
System.out.println(manService.sayEnglishHello());
}
}
运行结果:
你好,欢迎
hello,welcome
关于接口的,要在实现类上面添加注解说明。坑爹的,网上有篇文章说是要在接口上添加注解,不能在实现类上面,导致错误了半天。
关于注解的各个标签,可以单独百度一下,很多讲解。
spring 注解实例的更多相关文章
- Spring注解实例
public class ActivityAction extends CoreAction { private static final Logger log = Logger.getLogger( ...
- Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置
用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...
- Spring的AOP配置文件和注解实例解析
1.1 Spring的AOP配置文件和注解实例解析 AOP它利用一种称为"横切"的技术,将那些与核心业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减 ...
- spring注解说明之Spring2.5 注解介绍(3.0通用)
spring注解说明之Spring2.5 注解介绍(3.0通用) 注册注解处理器 方式一:bean <bean class="org.springframework.beans.fac ...
- 使用Spring注解来简化ssh框架的代码编写
目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写. 首先:我们浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="m ...
- [转]Spring 注解总结
原文地址:http://blog.csdn.net/wangshfa/article/details/9712379 一 注解优点?注解解决了什么问题,为什么要使用注解? 二 注解的来龙去脉(历史) ...
- spring 注解简单使用
一.通用注解 1.项目结构: 2.新建Person类,注解@Component未指明id,则后期使用spring获取实例对象时使用默认id="person"方式获取或使用类方式获取 ...
- Spring 注解总结
声明:这是转载的.内容根据网上资料整理.相关链接:http://www.360doc.com/content/10/1118/16/2371584_70449913.shtmlhttp://www.i ...
- Spring Security4实例(Java config版)——ajax登录,自定义验证
本文源码请看这里 相关文章: Spring Security4实例(Java config 版) -- Remember-Me 首先添加起步依赖(如果不是springboot项目,自行切换为Sprin ...
随机推荐
- java.lang.IllegalArgumentException: Service not registered
java.lang.IllegalArgumentException: Service not registered 首先检查一下,Service是否在AndroidManifest文件中注册.格式如 ...
- mysq 数据库基本管理
命令表: 1.设置mysql数据库密码 mysqladmin -u root password 'new-password' 'new-password'为数据库密码. 2.登陆mysql数据库 my ...
- sublimetext 2 编译文件带input时 提示 EOFError: EOF when reading a line
昨天在网下下载了个什么sublimetxt 2 的破解版,然后让我折腾了半天,没错 ,就是因为这个 EOFError: EOF when reading a line错误让我搞的半死.怨自己,贪图中文 ...
- C & C++ 宏与const
1.宏定义函数: 例:#define do{exp} while(0)与#define exp有什么不同,好处在哪里? 定义复杂代码,防止分号,或是括号不匹配等错误.比如: 定义: #define s ...
- 兼容多浏览器的网页复制插件(ZeroClipboard)
前言: 常规利用JS编写的网页复制功能是最简单的方法,但是只对IE有效,无法做到兼容其它浏览器,对其他浏览器也就只能弹窗提示用户手动复制了. <script type="text/ja ...
- MySQL优化update操作
http://www.cnblogs.com/jevo/p/3314928.html 用replace代替delete和insert. insert用批量. 用延迟(类似于缓冲,会否饿死) 大量插入, ...
- windows系统如何真正隐藏文件夹[转载]
方法一(推荐)eg:现需隐藏e盘bak目录下的tools文件夹e:\bak\tools运行:cmd键入:attrib +s +a +h +r e:\bak\tools然后,你再进去看e盘bak目录下, ...
- [leetcode]496. Next Greater Element I下一个较大元素
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- Golang之(for)用法
地鼠每次选好了一块地,打洞,坚持半个月发现地下有块石头,然后他就想绕路了...殊不知绕路只会让它离成果越来越远 package main import ( "fmt" " ...
- PAT 1073 多选题常见计分法(20)(代码+思路)
1073 多选题常见计分法(20 分) 批改多选题是比较麻烦的事情,有很多不同的计分方法.有一种最常见的计分方法是:如果考生选择了部分正确选项,并且没有选择任何错误选项,则得到 50% 分数:如果考生 ...