package com.xiami.spring;
public class HelloWorld {
private String str;
/**
* 默认构造方法
*/
public HelloWorld() {
}
/**
* 用来进行构造注入的构造方法
*
* @param str
*/
public HelloWorld(String str) {
this.str = str;
}
/**
* 用来进行Set注入的方法
* @param str
*/
public void setStr(String str) {
this.str = str;
}
/**
* 输出字符串的方法
*/
public void sayHello() {
System.out.println(str);
}
}
package com.xiami.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//载入spring配置文件
BeanFactory bFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
HelloWorld helloWorld = (HelloWorld) bFactory.getBean("helloService");
helloWorld.sayHello();
}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 使用Set方式注入 -->
<!--
<span style="color:#ff0000;"><bean id="helloService" class="com.xiami.spring.HelloWorld"
abstract="false" lazy-init="default" autowire="default">
<property name="str">
<value type="java.lang.String">hello world</value>
</property>
</bean></span>
-->
<!-- 使用构造方法方式注入
<bean id="helloService" class="com.xiami.spring.HelloWorld"
abstract="false" lazy-init="default" autowire="default">
<constructor-arg>
<value type="java.lang.String">构造方法注入方式</value>
</constructor-arg>
</bean>
-->
</beans>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有