spring 框架的一个技术点IOC

来源:互联网 发布:java中synchronized 编辑:程序博客网 时间:2024/06/07 05:19

写一个普通的实体类

package com.zhihao.bean;public class User {private int id;private String name;private String pwd;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 String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public String say(){return "[name:"+name+";pwd"+pwd+"]";}}

用一个文件myBean.xml把这个实体类注册

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:cache="http://www.springframework.org/schema/cache"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">       <bean id="user" class="com.zhihao.bean.User" scope="singleton">          <property name="name">              <value>yang</value>          </property>         <property name="pwd">              <value>123456</value>          </property>       </bean>  </beans> 

在web.xml里面把这个配置文件myBean.xml 包含进来

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>testAOP</display-name><listener><description>Spring监听器</description><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:myBean.xml</param-value></context-param><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/springmvc-servlet.xml</param-value></init-param><!-- <load-on-startup>1</load-on-startup> --></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list></web-app>


使用方法

package com.good.controller;import java.util.ArrayList;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.zhihao.bean.User;@Controllerpublic class YamuController {@AutowiredUser user;@ResponseBody@RequestMapping("/user")public List user(){/*ApplicationContext ct = new ClassPathXmlApplicationContext("/myBean.xml");User user = (User)ct.getBean("user");*/List list = new ArrayList();list.add("ya mu");list.add(user.say());System.out.println("在controller 进行业务处理,数据库的增删改查等等。。");return list;} }

这样就可以使用,用到了Spring的IOC技术。值得注意的是,在web.xml 里面,如果没有Spring 监听器 org.springframework.web.context.ContextLoaderListener,配置文件myBean.xml 不能产生作用。

原创粉丝点击