Spring包下载地址:http://repo.spring.io/release/org/springframework/spring/
jar包下载:http://www.manyjar.com/showJar/m.html
1.set方式注入
配置文件:
<?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.1.xsd">
<!-- 使用set方法注入属性
singleton 单实例 默认
prototype 多实例
-->
<bean id="user" class="com.spring.demo1.User" scope="singleton">
<!-- 注入属性值
name的属性值就是类里面定义的属性名称
value设置具体值
-->
<property name="bookname" value="程序设计"></property>
</bean>
</beans>
测试代码:
package com.spring.demo1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestIOC {
public static void main(String[] args) {
//1.加载spring配置文件,根据配置创建对象
ApplicationContext context = (ApplicationContext) new ClassPathXmlApplicationContext("config.xml");
//2.得到配置创建的对象
User user=(User) context.getBean("user");//强转,引号内为配置文件中的id名
User user1=(User) context.getBean("user");
System.out.println(user);//单实例输出地址
System.out.println(user1);//多实例输出地址不一样
user.add();//方法调用
}
}
package com.spring.demo1; public class User { private String bookname; //只需要构造一个set方法 public void setBookname(String bookname) { this.bookname = bookname; } public void add(){ System.out.println("bookname......"+bookname); } }
测试结果:
2.有参构造注入
配置文件:
<?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.1.xsd"> <bean id="HelloWorld" class="com.gc.action.HelloWorld"> <!-- 有参构造注入 --> <constructor-arg index="0"> <value>HelloWorld1</value> </constructor-arg> </bean> </beans>
测试代码:
package com.gc.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.gc.action.HelloWorld; public class TestHelloWorld { public static void main(String[] args) { ApplicationContext actx=(ApplicationContext) new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld helloworld=(HelloWorld) actx.getBean("HelloWorld"); System.out.println(helloworld.getMsg()); } }
package com.gc.action; public class HelloWorld { public String msg=null; //添加构造方法实现构造注入 public HelloWorld(String msg) { super(); this.msg = msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
结果:
新看了一个P名称空间注入:
config.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 引入p名称空间 ,最后以p结尾--> <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.1.xsd"> <!-- p名称空间注入 --> <bean id="person" class="com.spring.property.Person" p:name="lucy"></bean> </beans>
Person.java
package com.spring.property; public class Person { private String name; public void setName(String name) { this.name = name; } public void test1(){ System.out.println("person"+name); } }
Testp.java
package com.spring.property; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Testp { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("config.xml"); Person person=(Person) context.getBean("person"); person.test1(); } }
测试结果: