先貼spring的開發文檔,有助於大家學習http://shouce.jb51.net/spring/beans.html#beans-factory-class
一直想研究一下spring bean的控制反轉的實現,廢話不多說。
1、先建了一個WEB工程,導入相關spring的jar包,裝載到tomcat上,成功訪問,有不懂的童鞋可以移步http://www.cnblogs.com/mei0619/p/6560332.html。
2.為了方便研究,我將對象的調用直接寫在controller里,輸出相應的日志,如果可以輸出,證明bean是創建成功的,代碼如下:
package com.controller; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.entity.User; import com.entity.World; @Controller public class SpringServlet { /** * @Autowired 自動裝載world對象 自動按type裝載,也就是bean的class屬性 * 或者@Resource(name="world") 按name裝載,也就是bean的id */ @Resource(name="world") //這里的@Resource(name="world")和@Autowired是一模一樣的, public World world; @RequestMapping("/index") public String index(Model model){ world.worldTest(); return "index"; } @RequestMapping("/save") public String Save(@ModelAttribute("form") User user, Model model) { // user:視圖層傳給控制層的表單對象;model:控制層返回給視圖層的對象 System.out.println(model.toString()); model.addAttribute("user", user); return "detail"; } }
3、然后還寫了兩個類,代碼如下:
package com.entity; import java.io.Serializable; import java.util.Date; import org.springframework.stereotype.Repository; @Repository public class User implements Serializable{ private static final long serialVersionUID = 1L; private Integer id; // id private String name; // name private String pwd; // pwd private Integer age; // age // private Date creatTime; // creatTime public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } // public Date getCreatTime() { // return creatTime; // } // // public void setCreatTime(Date creatTime) { // this.creatTime = creatTime; // } public String toString(){ return name+"333"+age; } public void test(){ System.out.println("22222"); } }
package com.entity; import java.io.Serializable; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; public class World implements Serializable{ private static final long serialVersionUID = 1L; // @Resource(name="user") //這里的@Resource(name="user")和@Autowired是一模一樣的, private User user; private String type; public String getType() { return type; } public void setType(String type) { this.type = type; } public void setUser(User user) { this.user = user; } public void worldTest(){ user.test(); System.out.println("worldTest------" + type+"------"+user.getName()); } }
在上述代碼中,SpringServlet里的world是IOC容器通過標簽@Resource從配置文件中根據name裝載的,而World類里同樣有一個User類,這個類沒有加標簽,他是通過setUser()的方法裝載的user bean類,配置文件里配置一下bean類,代碼如下:
<bean id="world" class="com.entity.World"> <property name="user" ref="user" /> <property name="type" value="stringWorld"></property> </bean> <bean id="user" class="com.entity.User"/>
這樣,SpringServlet里的world就通過控制反轉的方式生成對象,然后調用world的worldTest()方法,就可以成功輸出,如果world對象沒有成功生成,調用他的方法的時候會報 java.lang.NullPointerException,
后台日志打印:
證明對象已經成功創建,如果想給對象添加屬性,可以直接在配置文件里添加,如下形式:
<bean id="user" class="com.entity.User"> <property name="id" value="1"></property> <property name="name" value="guan"></property> <property name="pwd" value="111111"></property> <property name="age" value="12"></property> </bean>
最后,我想讓代碼更精簡,不想寫那么多配置文件,可以采用注解的形式,為World.java的User屬性添加@Autowired或者@Resource(name="user")標簽,然后改一下配置文件,只需要寫兩行,如下:
<bean id="world" class="com.entity.World" /> <bean id="user" class="com.entity.User"/>
是不是代碼精簡了好多,也裝逼了好多,程序可以成功運行,同時日志台輸出:
然后就結束了,很多東西還有待學習,,,有很多不明白,最后貼一下配置文件springSerblet-config.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd "> <!-- 使用默認的注解映射 --> <mvc:annotation-driven /> <!-- <mvc:resources location="/" mapping="/index.html" /> <context:annotation-config></context:annotation-config>--> <!-- world bean類 --> <bean id="world" class="com.entity.World" /> <!-- <property name="user" ref="user" /> <property name="type" value="stringWorld"></property> </bean> --> <bean id="user" class="com.entity.User"/> <!-- user bean類 --> <!-- <bean id="user" class="com.entity.User"> <property name="id" value="1"></property> <property name="name" value="guan"></property> <property name="pwd" value="111111"></property> <property name="age" value="12"></property> </bean> --> <!-- <bean id="world" class="com.entity.World"> <property name="user"> <idref bean="user"/> </property> <property name="type" value="stringWorld"></property> </bean> --> <!-- 自動掃描controller包中的控制器 --> <context:component-scan base-package="com.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/index.do">controllerDoem</prop> </props> </property> </bean> <bean id="controllerDoem" class="com.controller"> <property name="view"> <value>index</value> </property> </bean>--> </beans>
applicationContext.xml和web.xml里的是一些web工程基本配置,不要動它,整體結構是這樣的:
最后,歡迎各位大神指導,指出錯誤和不足。。。。 正在努力學習中。。。