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 ...
随机推荐
- 【HDU 5818多校】Joint Stacks
用两个栈模拟,并保存每个点的时间戳.每次合并的时候记录合并时的时间戳mcnt和此时的topa和topb记做ta.tb. 每次pop的时候,如果栈的top的时间戳大于mcnt,则普通地pop,否则就在两 ...
- Leetcode 77, Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- asp.net下webform的ReadOnly和Enabled属性最终渲染的结果
ReadOnly对应readonly="readonly" Enabled对应disabled="disabled" 然后研究了一下这两种的用法,特此标记一下: ...
- Linux内核版本类型
对于Linux内核发布的版本类型有如下,也是自己的理解: [mainline]:主线版本,由Linux Torvalds维护和发布. [stable/EOL]:稳定版本,每个由主线发布的版本都叫做稳定 ...
- UVa 11384 Help is needed for Dexter
分析题目以后得出,对于一个连续等差递增的序列,例如1,2,3,4,5,6,每次选择其中后一半,减去能减的最大数,则是最优操作. 上述序列经过一次操作后变为1,2,3,0,1,2,此时可抛弃后一半(已经 ...
- C语言用面向对象的思想写贪吃蛇
大概一年前这时候,接触C语言一个月,那时候知之甚少,对面向对象只觉”可远观而不可亵玩“,而且会看到很多言论说C语言就是面向过程的语言,C++就是面向对象的语言.不过,不记得什么时候在网上看到过一篇博文 ...
- Xpath用法
在进行网页抓取的时候,分析定位html节点是获取抓取信息的关键,目前我用的是lxml模块(用来分析XML文档结构的,当然也能分析html结构), 利用其lxml.html的xpath对html进行分析 ...
- HDU 5904 LCIS (最长公共上升序列)
传送门 Description Alex has two sequences a1,a2,...,an and b1,b2,...,bm. He wants find a longest common ...
- UVA1213Sum of Different Primes(素数打表 + DP)
题目链接 题意:选择k个素数,使得和为N(1120)的方案数: 筛选出 <= N 的素数,然后就背包 写的时候没初始dp[0][0] = 1;而且方案数也没相加,真是弱逼 #include &l ...
- javamail实践
public static void main(String[] args) throws Exception, Exception { MimeMessage message=new MimeMes ...