zoukankan      html  css  js  c++  java
  • spring自动注入bean

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    	<display-name></display-name>
    	<welcome-file-list>
    		<welcome-file>index.jsp</welcome-file>
    	</welcome-file-list>
    	<servlet>
    		<servlet-name>spring</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>spring</servlet-name>
    		<url-pattern>*.do</url-pattern>
    	</servlet-mapping>
    </web-app>
    

     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:p="http://www.springframework.org/schema/p"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="       
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd     
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd      
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd       
      http://www.springframework.org/schema/context    
      http://www.springframework.org/schema/context/spring-context-2.5.xsd">
      
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver">
    		</property>
    		<property name="url" value="jdbc:mysql://127.0.0.1:3306/pecope">
    		</property>
    		<property name="username" value="root"></property>
    		<property name="password" value="root"></property>
    	</bean>
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.MySQLDialect
    				</prop>
    			</props>
    		</property>
    		<property name="annotatedClasses">
    			<list>
    				<value>com.zbb.entity.Salesman</value>
    				<value>com.zbb.entity.Plan</value>
    				<value>com.zbb.entity.Schedule</value>
    				<value>com.zbb.entity.Room</value>
    				<value>com.zbb.entity.Customer</value>
    				<value>com.zbb.entity.Message</value>
    				<value>com.zbb.entity.Material</value>
    				<value>com.zbb.entity.Project</value>
    				<value>com.zbb.entity.Bargain</value>
    				<value>com.zbb.entity.Plan2</value>
    			</list>
    		</property>
    	</bean>
    	<context:component-scan base-package="com.zbb" use-default-filters="false">
    		<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 
    		<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> 
    		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
    	 </context:component-scan>
    	
    	
    	
    	
    	
    </beans>
    

     BaseAction.java

    package com.zbb.action;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.zbb.entity.Customer;
    import com.zbb.iservice.IBaseService;
    
    @Controller
    @RequestMapping(value="/baseAction")
    public class BaseAcion {
    	@Autowired
    	IBaseService iBaseService;
    
    	@RequestMapping(value="/getAll")
    	public String getAll() {
    		System.out.println("123");
    		iBaseService.getAll(Customer.class);
    		return "/success.jsp";
    	}
    
    }
    

     IBaseDao.java

    若结合hibernatedaosupport查询数据库则要加上红色标记的部分不然出现异常,而且要注意懒加载问题

    @Autowired  
        public void setMySessionFactory(SessionFactory sessionFactory){  
            super.setSessionFactory(sessionFactory);  
        }

    package com.zbb.idao;
    
    import java.sql.SQLException;
    import java.util.List;
    
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.orm.hibernate3.HibernateCallback;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import org.springframework.stereotype.Repository;
    @Repository
    public class IBaseDao extends HibernateDaoSupport{
        
        public List<Object> getAll(final Class cls) {
            // TODO Auto-generated method stub
            System.out.println("basedao");
            return super.getHibernateTemplate().executeFind(new HibernateCallback() {
    
                public Object doInHibernate(Session arg0)
                        throws HibernateException, SQLException {
                    // TODO Auto-generated method stub
                    return arg0.createQuery("from "+cls.getSimpleName()).list();
                }
            });
        }
    @Autowired  
        public void setMySessionFactory(SessionFactory sessionFactory){  
            super.setSessionFactory(sessionFactory);  
        }
    }

     IBaseService.java

    package com.zbb.iservice;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.zbb.idao.IBaseDao;
    @Service
    public class IBaseService{
    	@Autowired
    	IBaseDao iBaseDao;
    	public List<Object> getAll(Class cls) {
    		// TODO Auto-generated method stub
    		System.out.println("baseservice");
    		iBaseDao.getAll(cls);
    		return null;
    	}
    
    }
    

     Test.java

    package com.zbb.iservice;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.zbb.idao.IBaseDao;
    @Service
    public class IBaseService{
    	@Autowired
    	IBaseDao iBaseDao;
    	public List<Object> getAll(Class cls) {
    		// TODO Auto-generated method stub
    		System.out.println("baseservice");
    		iBaseDao.getAll(cls);
    		return null;
    	}
    
    }
    
  • 相关阅读:
    Java WebService入门实例
    Maven是什么
    for 循环
    2.4 DevOps工程师的故事:构建运行时的严谨性
    2.3.3 构建微服务的入口:Spring Boot控制器
    2.3.2 引导Spring Boot应用程序:编写引导类
    2.1.3 互相交流:定义服务接口
    第2章 使用Spring Boot构建微服务
    第1章 欢迎来到Cloud和Spring
    第一次在博客园开博
  • 原文地址:https://www.cnblogs.com/super-admin/p/5515654.html
Copyright © 2011-2022 走看看