導入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 } }