Foreword
Record their own learning process
Understanding of the Spring Framework
Logical layer Spring framework developed popular
features:
-
Noninvasive
the API Spring Framework does not appear on the business logic
for the application, the business logic can release from the current application
for a frame, a rapid migration from the business logic to speak to other frameworks Spring Framework -
Container
vessel function can manage the life cycle of objects, dependencies between objects
can write the name of a configuration file (xml file) definition of an object, whether it is a single case, set dependencies with other objects -
IOC
inversion of control, that is dependent on the transfer relationship, if previously dependent realize, now reverse is dependent on the abstract, the core idea is the face of the programming interface -
Dependency injection
to achieve dependency between the object and the object -
AOP (face section Programming)
logs, security, transaction management and other services understood as a "slice", can achieve reuse, dynamically inserted will cut business logic, business logic can easily make use of the service section provides
specific content explanation: Spring Teaching Gangster
The contents of the Spring Framework
- Spring Core portion into the container as dependent
- Spring AOP Declarative transaction
- Spring ORM support for Hibernate persistence framework, etc.
- Spring Context provides a convenient and integrated tools for enterprise development
- Spring Web provides support for Web application development
Simple application
1. Create the IDEA project
2. A simple application
(1) HelloWorld class
package com;
public class HelloWorld {
private String userName;
public void setUserName(String userName){
this.userName=userName;
}
public void show(){
System.out.print(userName+":欢迎学习Spring");
}
}
(2) applicationContext.xml
by instantiating Helloworld bean element class, id attribute identifies the instance name helloworld, class attribute specifies the full path to be instantiated class name, property class attribute assignment
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloworld" class="com.HelloWorld">
<property name="userName" value="zhangsan"></property>
</bean>
(3) Test class TestHelloWorld
package com.show;
import com.HelloWorld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHelloWorld {
public static void main(String [] args){
//加载xml配置
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//获得配置中的实例
HelloWorld helloWorld=(HelloWorld)ctx.getBean("helloworld");
//调用方法
helloWorld.show();
}
}
(4) test
Spring core mechanism understood: dependency injection / inversion of control
(1) when another instance needs java java example, the caller is an example of a conventional method to create the callees (new instance obtained), using dependency injection, the example is completed by the caller Spring container, and then injected into the caller
(2) oriented programming interface: defining component interfaces, independent development of the various components, according to the operation of the assembly component dependencies
Examples understand (a simple login verification)
(1) create an interface UserDao
package com.Dao;
public interface UserDao {
public boolean login(String loginName,String loginPwd);
}
(2) the interface implementation class UserDaoImpl UserDao
username is admin, password 123456 to log in successfully
package com.Dao.Impl;
import com.Dao.UserDao;
public class UserDaoImpl implements UserDao {
@Override
public boolean login(String loginName, String loginPwd) {
if (loginName.equals("admin")&&loginPwd.equals("123456")){
return true;
}
else return false;
}
}
(. 3) Service layer
with Service interface is to decouple, said decoupling means is a layer change the code, the code will not affect other layers, i.e. oriented programming interface
Service class encapsulates business process, the DAO class encapsulates access to persistent layer
The final project performance level call control layer, the control layer to call the business layer, said call data access layer, business layer
package com.Dao.Service;
public interface UserService {
public boolean login(String loginName,String loginPwd);
}
(4) create a class that implements the interface UserServiceImpl of UserService
package com.Dao.Impl;
import com.Dao.Service.UserService;
import com.Dao.UserDao;
public class UserServiceImpl implements UserService {
UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao=userDao;
}
@Override
public boolean login(String loginName, String loginPwd) {
return userDao.login(loginName,loginPwd);
}
}
No traditional new new UserDaoImpl () in the above code example way to obtain UserDaoImpl data access layer class, only the interface declares UserDao userDao object, and adds the set () method for dependency injection
UserDaoImpl class instantiation and completion of the injection of the object UserDao in xml
(4) xml configuration file
configuration attributes associated with the tag bean
<!--创建userDapImpl的实例-->
<bean id="userDao" class="com.Dao.Impl.UserDaoImpl">
</bean>
<!--配置UserServiceImpl的实例-->
<bean id="userService" class="com.Dao.Impl.UserServiceImpl">
<!--依赖注入数据访问层组件-->
<property name="userDao" ref="userDao"></property>
</bean>
UserDaoImpl bean element creates an instance of
another example UserServiceImpl bean element created using a name attribute set to the property userDao, representative of the class UserServiceImpl userDao need to inject property value, or value designated by ref, ref denotes a container for the IOC examples bean references cited herein UserDaoImpl examples of
(5) test class (testlogin)
package com.show;
import com.Dao.Service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestLogin {
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml ");
//获取配置中UserServiceImpl的实例
UserService userService=(UserService)ctx.getBean("userService");
boolean flag=userService.login("admin","123456");
if(flag){
System.out.print("登录成功");
}
else System.out.print("登录失败");
}
}
Results can be obtained:
in the test class, xml configuration file by loading Spring ClassPathXmlApplicationContext class instance UserServiceImpl class obtained from the configuration file, the last call login () method, run the test class console output