package cn.cbb.ioc; public class User { public void add() { System.out.println("add ..."); } public static void main(String[] args) { /** * 調用類中的方法,原始作法是建立類對象,用對象調用方法;而如今則再也不這樣作了, * 而是將對象的建立過程交給spring來配置實現,可是底層用的確定是反射,咱們不需 * 要寫反射代碼,由spring代咱們作此操做 */ User user = new User(); user.add(); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- ioc入門,配置對象的建立 --> <bean id="user" class="cn.cbb.ioc.User"></bean> </beans>
package cn.cbb.ioc; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class IOCTest { @Test public void IOCTest() { //一、加載spring配置文件,根據配置建立對象 ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); //二、獲得配置建立的對象 User user = (User) context.getBean("user"); System.out.println(user); user.add(); } }
Spring引入schema約束,把約束文件引入到eclipse中,以便配置文件編寫提示:
java
<!-- ioc入門,配置對象的建立 --> <bean id="user" class="cn.cbb.ioc.User"></bean>
對象對應的類中,若沒有無參構造(定義了有參構造),則將出現異常:mysql
org.springframework.beans.factory.BeanCreationException:...No default constructor found;...
<!-- 使用靜態工廠建立對象 --> <bean id="bean2Factory" class="cn.cbb.bean.Bean2Factory" factory-method="getBean2"></bean>
package cn.cbb.bean; public class Bean2Factory { //靜態方法返回Bean2對象 public static Bean2 getBean2() { return new Bean2(); } }
<!-- 使用實例工廠建立對象 --> <!-- 首先,建立工廠對象 --> <bean id="bean3Factory" class="cn.cbb.bean.Bean3Factory"></bean> <!-- 其次,經過實例化的工廠對象的方法來建立對象bean3 --> <bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
package cn.cbb.bean; public class Bean3Factory { //普通方法返回Bean3對象 public Bean3 getBean3() { return new Bean3(); } }
User user1 = (User) context.getBean("user"); User user2 = (User) context.getBean("user"); System.out.println(user1); System.out.println(user2);
<!-- 默認scope="singleton" --> <bean id="user" class="cn.cbb.ioc.User"></bean>
cn.cbb.ioc.User@b7dd107
cn.cbb.ioc.User@b7dd107
<!-- scope="prototype"測試 --> <bean id="user" class="cn.cbb.ioc.User" scope="prototype"></bean>
cn.cbb.ioc.User@b7dd107
cn.cbb.ioc.User@42eca56e
<!-- 使用set方法注入屬性 --> <bean id="book" class="cn.cbb.property.Book"> <!-- 注入屬性之: name屬性值:類中的屬性名稱; value屬性:設置的具體值 --> <property name="bookName" value="紅樓夢"></property> </bean>
package cn.cbb.property; public class Book { private String bookName; //set方法 public void setBookName(String bookName) { this.bookName = bookName; } public void testBook() { System.out.println("bookName經過set注入值爲:"+bookName); } }
<!-- 使用有參數構造注入屬性 --> <bean id="propertyDemo1" class="cn.cbb.property.PropertyDemo1"> <constructor-arg name="userName" value="小碼"></constructor-arg> </bean>
package cn.cbb.property; public class PropertyDemo1 { private String userName; public PropertyDemo1(String userName) { this.userName = userName; } public void test1() { System.out.println("userName經過有參構造注入值爲:"+userName); } }
package cn.cbb.ioc; public class UserService { //定義dao類型屬性 private UserDao userDao; //生成set方法,在service中獲得dao對象,就能夠調用dao中的方法 public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add() { System.out.println("service add ..."); userDao.add(); } }
<!-- 注入對象類型的屬性 --> <!-- 配置service和dao對象 --> <bean id="userDao" class="cn.cbb.ioc.UserDao"></bean> <bean id="userService" class="cn.cbb.ioc.UserService"> <!-- 注入dao對象: name屬性值:service中的屬性名稱,對象類型屬性就不要寫value了,用ref屬性:dao配置bean的id值 --> <property name="userDao" ref="userDao"></property> </bean>
package cn.cbb.property; public class Person { private String pname; public void setPname(String pname) { this.pname = pname; } public void testP() { System.out.println("P名稱空間注入pname值爲"+pname); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- P名稱空間注入 --> <bean id="person" class="cn.cbb.property.Person" p:pname="小張"></bean> </beans>
package cn.cbb.property; import java.util.List; import java.util.Map; import java.util.Properties; public class Animal { private String[] arrs; private List<String> list; private Map<String,String> map; private Properties properties; public void setArrs(String[] arrs) { this.arrs = arrs; } public void setList(List<String> list) { this.list = list; } public void setMap(Map<String, String> map) { this.map = map; } public void setProperties(Properties properties) { this.properties = properties; } public void testAnimal() { System.out.println("複雜數據注入arrs:"+arrs); System.out.println("複雜數據注入list:"+list); System.out.println("複雜數據注入map:"+map); System.out.println("複雜數據注入properties:"+properties); } }
<!-- 注入複雜類型屬性值 --> <bean id="animal" class="cn.cbb.property.Animal"> <!-- 數組 --> <property name="arrs"> <list> <value>小王</value> <value>小李</value> <value>小張</value> </list> </property> <!-- list --> <property name="list"> <list> <value>奧迪</value> <value>寶馬</value> <value>奔馳</value> </list> </property> <!-- map --> <property name="map"> <map> <entry key="aa" value="lucy"></entry> <entry key="bb" value="lili"></entry> <entry key="cc" value="tom"></entry> </map> </property> <!-- properties --> <property name="properties"> <props> <prop key="driverclass">com.mysql.jdbc.Driver</prop> <prop key="username">root</prop> </props> </property> </bean>
//一、加載spring配置文件,根據配置建立對象 ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
new對象,功能能夠實現,可是效率過低!web