1属性注入。其实就是把对象里属性的值从xml读取出来,然后通过set方法设置到对象中。
开始
1、新建项目,spring_ioc
2、新建一个类User,包是com.spring.model
代码如下
package com.spring.model;
public class User {
private int userId;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
private String userName;
}
这是一个model类,作用就不说了,现在要从xml中把值注入进去
3 新建xml文件名springbeans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="user" class="com.spring.model.User">
<property name="userId" value="1"></property>
<property name="userName" value="张三"></property>
</bean>
</beans>
4新建一个测试类,为了简单就不适用junit了,包名com.spring.model.test类名UserTest
代码如下
package com.spring.model.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.model.User;
public class UserTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("springbeans.xml");
User user = (User) ctx.getBean("user");
System.out.println(user.getUserId());
System.out.println(user.getUserName());
}
}
这里注意,必须用ctx.getbean这种方式拿到对象,如果直接User user=new User();是无法注入属性的值的。
在这里会发现代码处错误,是因为还没有引入jar包
5引入jar包
引入spring.jar包
运行还是不行,因为还需要common-logging的jar包,再引入这个jar,这里我用的是1.2版本
引入jar包后的图
执行UserTest.java ok了
运行结果如下
2014-8-25 23:05:25 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61b383e9: display name [org.springframework.context.support.ClassPathXmlApplicationContext@61b383e9]; startup date [Mon Aug 25 23:05:25 HKT 2014]; root of context hierarchy
2014-8-25 23:05:25 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [springbeans.xml]
2014-8-25 23:05:26 org.springframework.context.support.ClassPathXmlApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@61b383e9]: org.springframework.beans.factory.support.DefaultListableBeanFactory@3d3e58d4
2014-8-25 23:05:26 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3d3e58d4: defining beans [user]; root of factory hierarchy
1
张三
- 大小: 23.8 KB
分享到:
相关推荐
IOC与DI的理解及使用 控制反转IOC(Inversion of Control)是一种设计思想,DI(依赖注入)是实现IOC的一种方法 。... 在Spring中实现控制反转的是IOC容器 ,其 实现方法是依赖注入 (Dependency Injection,DI)
当spring容器创建对象的时候,通过xml配置文件,把需要赋值的JavaBean属性设置在配置文件中,这样就可以把值赋到了对应的属性上。 – 成员属性和JavaBean属性: JavaBean属性=成员属性吗?答案是否定的,成员属性是...
《Spring3.x企业应用开发实战》是在《精通Spring2.x——企业应用开发详解》的基础上,经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练...
《Spring3.x企业应用开发实战》是在《精通Spring2.x——企业应用开发详解》的基础上,经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练...
使用Spring2.5的Autowired实现注释型的IOC , 使用Spring2.5的新特性——Autowired可以实现快速的自动注入,而无需在xml文档里面添加bean的声明,大大减少了xml文档的维护
11.4理解spring的核心——ioc 234 11.4.1控制反转 234 11.4.2依赖注入的3种方式 236 11.5beanfactory与applicationcontext 238 11.5.1认识beanfactory 238 11.5.2使用applicationcontext 238 11.6项目实战——...
2.2.2 struts 2属性配置文件struts.properties详解 55 2.2.3 struts 2核心配置文件struts.xml详解 57 2.3 struts 2应用开发实务 61 2.3.1 struts 2应用开发环境的搭建 62 2.3.2 struts 2应用基本开发步骤—搭建...
第1篇为技能学习篇,主要包括Java Web开发环境、JSP语法、JSP内置对象、Java Bean技术、Servlet技术、EL与JSTL标签库、数据库应用开发、初识Struts2基础、揭密Struts2高级技术、Hib锄劬e技术入门、Hibernate高级应用...
Spring MVC缺点: 大量的XML配置文件,太过灵活——没有公共的父控制器,没有内置的Ajax支持。 2 HTML语言: 超文本标记语言,标准通用标记语言下的一个应用。"超文本"就是指页面内可以包含 图片、链接,甚至音乐、...
第1章 Struts 2:现代Web框架 2 1.1 Web应用程序:快速学习 2 1.1.1 构建Web应用程序 2 1.1.2 基础技术简介 3 1.1.3 深入研究 6 1.2 Web应用程序框架 7 1.2.1 什么是框架 7 1.2.2 为什么使用框架 8 1.3 Struts 2框架...
动态调用对象的属性和方法——性能和灵活性兼备的方法 消除由try/catch语句带来的warning 微软的应试题完整版(附答案) 一个时间转换的问题,顺便谈谈搜索技巧 .net中的正则表达式使用高级技巧 (一) C#静态成员和...
由于J2EE的开源的框架中提供了MVC模式实现框架Struts、对象关系模型中的Hibernate 的框架及拥有事务管理和依赖注入的Spring。利用现存框架可以更快开发系统。所以选择Java技术作为blog 的开发工具。 为了增加系统的...