Spring入门_04_注解注入
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!--打开注解开关-->
<context:annotation-config/> <bean id="userAction" class="com.umgsai.spring.UserAction" scope="singleton"/> <bean id="userManager" class="com.umgsai.spring.UserManager" scope="prototype">
<property name="username" value="root">
</property>
<property name="password" value="123456">
</property>
<property name="url" value="jdbc:mysql://localhost:8080/3305">
</property>
<property name="driverClass" value="jdbc.mysql.Driver">
</property>
</bean> </beans>
package com.umgsai.spring;
import javax.annotation.Resource;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
public class UserAction implements BeanFactoryAware{
@Resource//省略set方法
private UserManager userManager = null;
public UserAction(UserManager userManager){
super();
this.userManager = userManager;
} public UserAction(){
super();
} public String execute() {
User user = new User();
//userManager = (UserManager)factory.getBean("manager");
userManager.add(user);
System.out.println("userManager:"+userManager); return "success";
}
private BeanFactory factory = null;
public void setBeanFactory(BeanFactory factory) throws BeansException {
this.factory = factory;
}
}
测试
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserAction userAction = (UserAction)factory.getBean("userAction");
System.out.println(userAction.execute());
执行结果
root
123456
jdbc.mysql.Driver
jdbc:mysql://localhost:8080/3305
添加User操作
userManager:com.umgsai.spring.UserManager@177ba38f
success
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1256242
Spring入门_04_注解注入的更多相关文章
- java 静态代码块和spring @value等注解注入顺序
java 静态代码块和spring @value等注解注入顺序 问题所在 先上代码 java方法 @Value("${mf.cashost}") public static S ...
- Spring 3.0 注解注入详解
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- Spring系列之新注解配置+Spring集成junit+注解注入
Spring系列之注解配置 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率 你本来要写一段很长的代码来构造一个 ...
- Spring入门_03_构造注入
实体类 Student.java package com.umgsai.spring.entity; import java.util.Date; public class Student { pri ...
- Spring入门_02_属性注入
Spring 的set方法(属性)注入 UserAction类中设置属性和get.set方法.(实际上只需要set方法) private List list = null; private Set s ...
- [转]Spring通过@Value注解注入属性的几种方式
原文地址:https://blog.csdn.net/csujiangyu/article/details/50945486 ------------------------------------- ...
- Spring入门(6)-使用注解装配
Spring入门(6)-使用注解装配 本文介绍如何使用注解装配. 0. 目录 使用Autowired 可选的自动装配 使用Qualifier选择 1. 使用Autowired package com. ...
- Spring入门(二)— IOC注解、Spring测试、AOP入门
一.Spring整合Servlet背后的细节 1. 为什么要在web.xml中配置listener <listener> <listener-class>org.springf ...
- Spring入门注解版
参照博文Spring入门一,以理解注解的含义. 项目结构: 实现类:SpringHelloWorld package com.yibai.spring.helloworld.impl; import ...
随机推荐
- 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定
介绍背水一战 Windows 10 之 绑定 TemplateBinding 绑定 与 RelativeSource 绑定 与 StaticResource 绑定 示例1.演示 TemplateBin ...
- 【HDU 5698】瞬间移动(组合数,逆元)
x和y分开考虑,在(1,1)到(n,m)之间可以选择走i步.就需要选i步对应的行C(n-2,i)及i步对应的列C(m-2,i).相乘起来. 假设$m\leq n$$$\sum_{i=1}^{m-2} ...
- Windows Directory ACL Security Check By ACL Baseline
catalog . Windows NTFS ACL(MAC) Permission . How the System Uses ACLs . 服务器不安全ACL配置带来的攻击向量 . NTFS AC ...
- MVC-12 ActionMethodSelectorAttribute
ActionMethodSelectorAttribute 其实微软对方法的起名都比较规范和通俗易懂的,从名字上来看就知道这是方法选择器 我们在action上加上 HttpGet.HttpPost . ...
- C#常用异常类记录
从MSDN抄下来的 ArgumentException 传递到方法的非空参数是无效的. ArgumentNullException 传递给方法的参数为空. ArgumentOutOfRangeExc ...
- A.Kaw矩阵代数初步学习笔记 1. Introduction
“矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...
- Linux DHCP通过OPTION43为H3C的AP下发AC地址
对于DHCP服务,可以在很多平台上进行设置.那么这里我们就主要讲解一下在Linux DHCP服务器上通过option 43实现H3C的AP自动联系AC注册的相关内容.原来的DHCP Server是放在 ...
- python函数(五)
函数 1.函数基本语法及特性 背景提要 现在老板让你写一个监控程序,监控服务器的系统状况,当cpu\memory\disk等指标的使用量超过阀值时即发邮件报警, 你掏空了所有的知识量,写出了以下代码 ...
- 《JavaScript权威指南》学习笔记 第五天 window对象的方法。
前天和昨天大致浏览了犀牛书的函数.类与模块.正则表达式.JavaScript扩展.以及服务端的js.这些方面对于我目前的水平来说比较难,一些最基本的概念都不能领会.不过最复杂的知识占用平时使用的20% ...
- iOS - 类簇
类簇是在Objective-C中Foundation Framework中广泛使用的一种设计模式 1.发现类簇(Class Cluster)的踪迹 //*> 执行下面代码 id obj1 = [ ...