Spring for cainiao-first example, spring for cainiao

Source: Internet
Author: User

Spring for cainiao-first example, spring for cainiao
I. Overview
We used the factory for flexible configuration. Now, after Spring is used, this is handed over to IoC container management. We only need to match the XML file, which saves a lot of development time. We don't need to know the number of dynamic matching classes. Here we will give you an example. example
Ii. Code display.

The file directory is as follows:


Jar package to be referenced:

Spring. jar

Lib/log4j/log4j. jar

Lib/jakarta-commons/commons-logging.jar

File to be referenced

Log4j. properties

The following code is used:

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 // Our application is responsible for service locating // 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 ("James", "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.springframework.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>


Running result:


Iii. Comparison
The difference between these two classes is that UserManagerImpl. java and Client. java are written differently.
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 // Our application is responsible for service locating 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 brings us great convenience so that we can decouple objects during development. This makes maintenance easier. When you need to modify the configuration file, you only need to modify the configuration file and add the object. You do not need to modify the written code.


I am a newbie. I want to learn how to remotely call Http invoker of the Spring framework. Here is a simple example.
Which of the following heroes helps me explain what the spring framework does? I am a beginner in JAVA and want to use the most common examples and explanations

Spring is generally used to associate two objects. objects and objects do not need to be instantiated. You only need to configure them in the Spring configuration file to determine the dependency between objects.

Spring has two major features: dependency injection and control inversion. in simple words, after you instantiate an object in the code, you have to call the object method. However, with Spring, you can direct the method to your objects and objects in the code. The advantage of this is that you only need to know the object name and do not need to know what the object is. Once the object changes, you only need to modify the configuration file.

Spring is an easy-to-understand and easy-to-understand Method for beginners. It can only be explained in this way. However, you must give an example with Spring to understand the mysteries of 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.