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 ...
随机推荐
- Android 最火开发框架 xUtils
xUtils简介 xUtils3 api变化较多, 已转至 https://github.com/wyouflf/xUtils3 xUtils 2.x对Android 6.0兼容不是很好, 请尽快升级 ...
- 自对齐(self-aligned)
C语言是自对齐的,32位以4字节对齐,64位以8字节对齐(1字节=8 bits) 自对齐的好处:在一条指令内完成数据的取或者存的操作,使得内存访问更快:否则,如果一个变量跨机器字存储,那么要做两次或更 ...
- 【原创】Junit4详解二:Junit4 Runner以及test case执行顺序和源代码理解
概要: 前一篇文章我们总体介绍了Junit4的用法以及一些简单的测试.之前我有个疑惑,Junit4怎么把一个test case跑起来的,在test case之前和之后我们能做些什么? Junit4执行 ...
- Oracle 进入数据库 新增用户 修改密码方法
1.以管理员身份进入 SQL plus 2.输入:sys/manager as sysdba 3.新增用户: 创建新用户: create user 用户名 identified by 密码; 解锁用户 ...
- WCF的例子
Demo的 “Service端”以本机IIS为宿主,“Client端”以WebForm项目为例. 1.新建项目:WCF>WCF Service Application: 2.删除默认文件ISer ...
- Linux 添加硬盘
一.简介 本文介绍为Linux 添加硬盘的基本方法,同时适用于为虚拟机添加硬盘的情况. 二.添加小于2T的硬盘 1)分区 fdisk /dev/hda 2)建立文件系统 3)设置开机自动挂载磁盘 ...
- 向一个文件流写入一个数据块---fwrite
函数原型:int fwrite(const void *buffer,size_t size,size_t count,FILE *stream); 参数说明:buffer:用于写入到文件的数据地址. ...
- WebService安全加密
众所周知,WebService访问API是公开的,知道其URL者均可以研究与调用.那么,在只允许注册用户的WebService应用中,如何确保API访问和通信的安全性呢?本文所指的访问与通信安全性包括 ...
- 判定map中是否存在某元素
判断某key是否存在可以使用map的count方法来间接判定 count接受一个参数key值,返回map中key值为给定值的元素总数 map<int, string> i_to_s_map ...
- 2018.09.24 bzoj4977: [[Lydsy1708月赛]跳伞求生(贪心+线段树)
传送门 线段树好题. 这题一看我就想贪心. 先把a,b数组排序. 然后我们选择a数组中最大的b个数(不足b个就选a个数),分别贪心出在b数组中可以获得的最大贡献. 这时可以用线段树优化. 然后交上去只 ...