The first example of a novice learning spring--

Source: Internet
Author: User

I. Overview
We used the factory to achieve flexible configuration. Now, with spring, this is given to IOC container management. We just have to put it on the XML file, so we can save a lot of development time. We don't need to know how much back there is. As long as the dynamic matching with the class can be done below to give you an example
Second, the code display.

File directories such as:


The jar package that needs to be referenced:

Spring.jar

Lib/log4j/log4j.jar

Lib/jakarta-commons/commons-logging.jar

Files that need to be referenced

Log4j.properties

Here's the code:

Userdao.java

Package Com.bjpowernode.spring.dao;public interface Userdao {public void AddUser (String username,string password);}


Userdao4mysqlimpl.java

Package Com.bjpowernode.spring.dao;public class Userdao4mysqlimpl implements Userdao {public void AddUser (String UserName, String password) {//TODO auto-generated method StubSystem.out.println ("Userdao4mysqlimpl.adduser ()");}}


Userdao4orcleimpl.java

Package Com.bjpowernode.spring.dao;public class Userdao4orcleimpl implements Userdao {public void AddUser (String UserName, String password) {//TODO auto-generated method StubSystem.out.println ("Userdao4orcleimpl.adduser ()");}}


Usermanager.java

Package Com.bjpowernode.spring.manager;public interface Usermanager {public void AddUser (String username,string password);}


Usermanagerimpl.java

Package Com.bjpowernode.spring.manager;import Com.bjpowernode.spring.dao.userdao;public Class UserManagerImpl Implements Usermanager{private Userdao userdao;public void Setuserdao (Userdao userdao) {This.userdao = UserDao;} Public Usermanagerimpl (Userdao userdao) {////this.userdao = Userdao;//}public void AddUser (String userName, String Password) {//TODO auto-generated method stub////is serviced by our application to locate//userdao userdao=new Userdao4mysqlimpl (); Userdao.adduser (userName, password);}}


Client.java

Package Com.bjpowernode.spring.client;import Org.springframework.beans.factory.beanfactory;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.bjpowernode.spring.dao.userdao4mysqlimpl;import Com.bjpowernode.spring.manager.usermanager;import Com.bjpowernode.spring.manager.usermanagerimpl;public class Client {/** * @param args */public static void main (string[] args) {beanfactory factory=new classpathxmlapplicationcontext ("Applicationcontext.xml"); Usermanager usermanager= (Usermanager) Factory.getbean ("Usermanager"); Usermanager.adduser ("Zhang San", "123");////TODO Auto-generated  Method Stub//usermanager usermanager=new Usermanagerimpl (New Userdao4mysqlimpl ());// Usermanager.adduser ("Zhang San", "1231");}}


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 "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:/ /www.springframework.org/schema/beans/spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www. Springframework.org/schema/aop/spring-aop-2.0.xsd Http://www.springframework.org/schema/tx Http://www.springfram Ework.org/schema/tx/spring-tx-2.0.xsd "><bean id=" Userdao4mysql "class=" Com.bjpowernode.spring.dao.UserDao4MySqlImpl "/><bean id=" userdao4orcle "class=" Com.bjpowernode.spring.dao.UserDao4OrcleImpl "/><bean id=" Usermanager "class=" Com.bjpowernode.spring.manager.UserManagerImpl "><!--<constructor-arg ref=" Userdao4mysql "/>- <property name= "Userdao" ref= "Userdao4mysql"/></bean></beans> 


Operation Result:


Third, Comparison
With the original factory write the difference is that the Usermanagerimpl.java and Client.java the two classes are not the same as the original two of these classes write
Usermanagerimpl.java

Package Com.bjpowernode.spring.manager;import Com.bjpowernode.spring.dao.userdao;import Com.bjpowernode.spring.dao.userdao4mysqlimpl;public class Usermanagerimpl implements Usermanager{public void AddUser (string userName, string password) {//TODO auto-generated method stub//by our application is responsible for service positioning Userdao userdao=new Userdao4mysqlimpl (); Userdao.adduser (UserName, password);}}


Client.java

Package Com.bjpowernode.spring.client;import Com.bjpowernode.spring.manager.usermanager;import Com.bjpowernode.spring.manager.usermanagerimpl;public class Client {/** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubusermanager usermanager=new Usermanagerimpl (); Usermanager.adduser ("Zhang San", "1231 ");}}


Iv. Summary

Spring has brought us a lot of convenience so that when we develop the object and object to understand the decoupling. Let us maintain more convenient, when the need to modify the time we just modify the configuration file, add the object can not modify the written code.

The first example of a novice learning spring--

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.