Spring Auto-inject bean

Source: Internet
Author: User

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://WW W.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 {@AutowiredIBaseService Ibaseservice, @RequestMapping (value= "/getall") public String GetAll () { System.out.println ("123"); Ibaseservice.getall (Customer.class); return "/success.jsp";}}

Ibasedao.java

If the combination of Hibernatedaosupport query database to add red marked part of the exception, and pay attention to lazy loading problem

@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; @Servicepublic class ibaseservice{@ Autowiredibasedao ibasedao;public list<object> getAll (Class cls) {//TODO auto-generated method StubSystem.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; @Servicepublic class ibaseservice{@ Autowiredibasedao ibasedao;public list<object> getAll (Class cls) {//TODO auto-generated method StubSystem.out.println ("Baseservice"); Ibasedao.getall (CLS); return null;}}

Spring Auto-inject bean

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.