导入jar包方法:右键project->properties->java build path->libaries->add external jars就行。java
准备pojo Product,用来测试IOC和DI程序员
package com.demo.pojo; public class Product{ private int id; private String name; //属性的getter和setter方法 public int getId(){ return id; } public void setId(int id){ this.id=id; } public String getName(){ return name; } public void setName(String name){ this.name=name; } }
applicationContext.xml是Spring的核心配置文件,经过配置bean的属性,根据关键字product来获取Product对象,该对象获取的时候,被注入了字符串"Apple"到属性中。spring
<?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/aop http://www.springframework.org/schema/aop/spring-aop-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"> <bean name="product" class="com.demo.pojo.Product"> <property name="name" value="Apple"/> </bean> </beans>
在test文件夹下,创建TestSpringapp
package com.demo.test; import org.springframework.context.ApplicationContext; import org.springframework.support.ClassPathXmlApplicationContext; import com.demo.pojo.Product; public class TestSpring{ public static void main(String[] args){ //加载applicationContext.xml配置文件 ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); //根据ApplicationContext对象提供的getBean()方法,获取配置bean的name属性值。 Product product=(Product)context.getBean("product"); System.out.println(product.getName()); } }
IOC方式获取对象方式:对象的生命周期交给Spring来管理,直接从Spring那里获取一个对象。框架
传统方式:经过new关键字主动建立一个对象。ide
以前直接对Product的name属性注入"Apple"字符串,这一次咱们注入一个Category分类对象。测试
package com.demo.pojo; public class Product{ private int id; private String name; private Category category;//类别属性 //属性的getter和setter方法 public int getId(){ return id; } public void setId(int id){ this.id=id; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public Category getCategory(){ return category; } pulic vodi setCategory(Category category){ this.category=category; } }
在建立Product的时候注入一个Category对象,这里要使用ref来注入另外一个对象。ui
<?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/aop http://www.springframework.org/schema/aop/spring-aop-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"> <bean name="product" class="com.demo.pojo.Product"> <property name="name" value="Apple"/> <property name="category" ref="Category"/><!--将category对象注入到product--> </bean> <!--配置Category的bean--> <bean name="category" class="com.demo.pojo.Category"> <property name="name" value="Fruit"/> </bean> </beans>
经过Spring获取Product对象被注入的Category对象this
package com.demo.test; import org.springframework.context.ApplicationContext; import org.springframework.support.ClassPathXmlApplicationContext; import com.demo.pojo.Product; public class TestSpring{ public static void main(String[] args){ //加载applicationContext.xml配置文件 ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); //根据ApplicationContext对象提供的getBean()方法,获取配置bean的name属性值。 Product product=(Product)context.getBean("product"); System.out.println(product.getName());//Apple System.out.pringln(product.getCategory().getName());//Fruit } }
添加配置:spa
<context:annotation-config/>
具体配置修改以下:
<?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/aop http://www.springframework.org/schema/aop/spring-aop-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"> <context:annotation-config/ <bean name="product" class="com.demo.pojo.Product"> <property name="name" value="Apple"/> <!--<property name="category" ref="Category"/><!--将category对象注入到product--><!--注释掉刚才给Product注入的category属性,后面采用注解完成该操做--> </bean> <!--配置Category的bean--> <bean name="Category" class="com.demo.pojo.Category"> <property name="name" value="Fruit"/> </bean>
2.一、在Product.java的Category属性上面加上@Autowired注解
package com.demo.pojo; public class Product{ private int id; private String name; @Autowried//采用注解注入Category属性,不用在xml配置bean的Product引入Category private Category category;//类别属性 //属性的getter和setter方法 public int getId(){ return id; } public void setId(int id){ this.id=id; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public Category getCategory(){ return category; } pulic vodi setCategory(Category category){ this.category=category; } }
2.二、也能够在setCategory方法上面加上@Autowired注解,达到一样效果
package com.demo.pojo; public class Product{ private int id; private String name; //@Autowried//采用注解注入Category属性,不用在xml配置bean的Product引入Category private Category category;//类别属性 //属性的getter和setter方法 public int getId(){ return id; } public void setId(int id){ this.id=id; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public Category getCategory(){ return category; } @Autowried pulic void setCategory(Category category){ this.category=category; } }
2.三、还能够采用@Resource注解
package com.demo.pojo; import javax.annotation.Resource; public class Product{ private int id; private String name; //@Autowried//采用注解注入Category属性,不用在xml配置bean的Product引入Category @Resource(name="category") private Category category;//类别属性 //属性的getter和setter方法 public int getId(){ return id; } public void setId(int id){ this.id=id; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public Category getCategory(){ return category; } //@Autowried pulic void setCategory(Category category){ this.category=category; } }
package com.demo.test; import org.springframework.context.ApplicationContext; import org.springframework.support.ClassPathXmlApplicationContext; import com.demo.pojo.Product; public class TestSpring{ public static void main(String[] args){ //加载applicationContext.xml配置文件 ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); //根据ApplicationContext对象提供的getBean()方法,获取配置bean的name属性值。 Product product=(Product)context.getBean("product"); System.out.println(product.getName());//Apple System.out.pringln(product.getCategory().getName());//Fruit } }
配置bean属性的都去掉,直接加上:
<context:component-scan base-package="com.demo.pojo"/>
做用:告诉Spring,bean都放在com.demo.pojo包下
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/aop http://www.springframework.org/schema/aop/spring-aop-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"> <context:component-scan base-package="com.demo.pojo"/> </beans>
4.一、使用@Component注解,为pojo加上注解,说明这个类是bean
Product.java:
package com.demo.pojo; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component("product") public class Product{ private int id; private String name="Apple"; //@Autowried//采用注解注入Category属性,不用在xml配置bean的Product引入Category @Resource(name="category") private Category category;//类别属性 //属性的getter和setter方法 public int getId(){ return id; } public void setId(int id){ this.id=id; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public Category getCategory(){ return category; } //@Autowried pulic void setCategory(Category category){ this.category=category; } }
Category.java
package com.demo.pojo; import org.springframework.stereotype.Component; @Component("category") public class Category { private int id; private String name="Fruit"; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4.二、TestSpring测试
package com.demo.test; import org.springframework.context.ApplicationContext; import org.springframework.support.ClassPathXmlApplicationContext; import com.demo.pojo.Product; public class TestSpring{ public static void main(String[] args){ //加载applicationContext.xml配置文件 ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); //根据ApplicationContext对象提供的getBean()方法,获取配置bean的name属性值。 Product product=(Product)context.getBean("product"); System.out.println(product.getName());//Apple System.out.pringln(product.getCategory().getName());//Fruit } }