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 ...
随机推荐
- $(this).form("validate") 始终返回false
onsubmit 提交前触发,返回 false 来阻止提交动作. validate 进行表单字段验证,当全部字段都有效时返回 true .该方法和 validatebox 插件一起使用. 解决:注释掉 ...
- Hook钩子编程
钩子(Hook),是Windows消息处理机制的一个平台,钩子实际上是一个处理消息的程序段,通过系统调用,把它挂入系统,以监视指定窗口的某种消息.每当特定的消息发出,在没有到达目的窗口前,钩子程序就先 ...
- 使用css实现特殊标志或图形
1. 前言 由于图片占的空间比较大,且图片越多,越不好管理,所以有些时候,我们可以使用一些简单的标签样式来实现简单的图形标志来替代图片. 2. 实例展示: 三角形示例 示例代码: <style ...
- [leetcode]314. Binary Tree Vertical Order Traversal二叉树垂直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- win10下安装oracle11G Examples出错[INS-32025][INS-52001]
安装oracle examples时提示出错:[INS-32025] 所选安装与指定 Oracle 主目录中已安装的软件冲突.[INS-52001] Oracle Database Examples ...
- memcache缓存失效
缓存过期 memcached在处理过期的缓存项时,采用懒惰模式处理方法. 缓存项过期,不会立即删除,直到对该缓存项执行了get操作,才会删除过期缓存. > set key 0 10 > t ...
- golang通过反射动态调用方法
func Call(m map[string]interface{}, name string, params ...interface{}) ([]reflect.Value, error) { f ...
- js取当前页面名称
// 取当前页面名称(不带后缀名) function pageName() { var a = location.href; var b = a.split(& ...
- linux下添加用户并赋予root权限
1.添加用户,首先用adduser命令添加一个普通用户,命令如下: #adduser tommy //添加一个名为tommy的用户#passwd tommy //修改密码Changing pass ...
- 一、消息队列之ActiveMQ的安装、配置和C#样例代码
最近有时间了,研究一下消息队列ActvieMQ,结合自己的实践和网上的一些大家内容,整理如下,所有步骤和链接均是正确的. 1.ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲 ...