spring属性的注入实例
来源:互联网 发布:魔兽之路隐藏英雄数据 编辑:程序博客网 时间:2024/05/16 04:17
spring框架核心jar包:
在工作目录下建立spring配置文件:
- setter注入
<?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:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> //定义一个bean,它名字叫P1,class后为实现类,属性name设置为micro,为非单例模式 <bean id="P1" class="micro.Person" destroy-method="close" scope = "prototype"> <property name="name" value = "micro" /> </bean></beans>
创建实体类Person:
package micro;/**@author:micro_hz2015年8月11日 */public class Person { String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}
测试代码:
package micro;import org.apache.catalina.core.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/**@author:micro_hz2015年8月11日 */public class Test { public static void main(String args[]) { //加载配置文件 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml"); //工厂模式得到该实例 Person p = (Person) context.getBean("P1"); System.out.println(p.getName()); }}
运行结果:
micro
- 构造注入:
spring.xml文件修改为:
<bean id="P1" class="micro.Person" destroy-method="close" scope = "prototype"> <constructor-arg value = "master"></constructor-arg> </bean>
实体类Person添加构造方法:
public class Person { String name; public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
测试:
public class Test { public static void main(String args[]) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml"); Person p = (Person) context.getBean("P1"); System.out.println(p.getName()); }}
输出:
master
0 0
- spring属性的注入实例
- Spring的属性注入到bean实例的细节
- Spring---->属性的注入
- 关于spring的属性注入
- Spring属性注入的方式
- Spring的属性注入(SPEL)
- spring 静态属性的注入
- Spring的xml注入实例
- Spring的annotation注入实例
- 使用spring进行属性注入的实例解析(spring面向切面和控制反转)。
- Spring属性注入Bean的List属性
- 【Spring 2】spring的属性注入形式
- [4]Spring以注解的方式进行Bean的实例化和属性注入
- 模拟spring,自定义容器一(属性注入、Bean的实例化)
- spring的静态属性的注入
- spring的pojo类属性的注入
- Spring各种属性的注入配置
- Spring整理4 -- 公共属性的注入
- leetcode 日经贴,python code -Different Ways to Add Parentheses
- GDOI2016模拟8.10火星菌
- 网络流 HDU 3549 Flow Problem
- 29-HTML-15-HTML(GET和POST区别)
- 学英语从背单词开始,但背单词要适可而止
- spring属性的注入实例
- 29-HTML-16-HTML(服务端GET和POST区别)
- 用python第三方库xlwt解决数据提取后存到execl
- 在虚拟机中安装windows
- 29-HTML-17-HTML(服务端和客户端校验的问题)
- 《开源公开课分享》:Java开源框架案例分享
- Jsoup解析Html教程
- 29-HTML-18-HTML(常见的其他标签)
- Android单元测试异常总结---Test run failed:Unable to find instrumentation target package