Spring (1) and Spring ()
I. Spring Overview
A long talk
Spring is an open-source framework. Spring was a lightweight Java development framework that emerged in 2003, it is derived from part of the concepts and prototype stated by Rod Johnson in his book "Expert One-On-One J2EE Development and Design. It is created to solve the complexity of enterprise application development. One of the main advantages of the Framework is its layered architecture, which allows users to choose which component to use and provides an integrated framework for J2EE application development. Spring uses the basic JavaBean to complete the previous tasks that may only be completed by EJB. However, Spring is not only used for server-side development. From the perspective of simplicity, testability, and loose coupling, any Java application can benefit from Spring. The core of Spring is IoC and AOP ). In short, Spring is a hierarchical JavaSE/EEFull-stack (All-in-One)Lightweight open-source framework.
Ii. Spring Architecture
2.1 architecture Introduction
- Spring Core: Manages dependencies between beans.
- Spring Web: supports Web Application Development
- Spring DAO: supports consistent Exception Handling and programming methods.
- Spring BeanFactory: a sub-interface with enhanced functionality
- Spring MVC: Implementation of Web application MVC
- Spring ORM: used for integration with popular ORM frameworks
- Spring AOP: AOP face aspect programming, which provides number implementation compatible with the OP Alliance
Most of the above can be used independently.
2.2 parts
Iii. Spring Design Philosophy
4. How to obtain Spring
Http://repo.spring.io/libs-release-local/org/springframework/spring/
5. Use Spring to implement Hello Spring
Add package
Create a class
Package com. pb. demo; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; public class HelloSpring {private String str; public void print () {System. out. println ("Hello! "+ This. getStr ();} public static void main (String [] args) {// create an ApplicationContext file object ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext. xml "); HelloSpring hello = (HelloSpring) context. getBean ("hellospring"); hello. print ();} public String getStr () {return str;} public void setStr (String str) {this. str = str ;}}
Create an applicationContext. xml configuration file
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="hellospring" class="com.pb.demo.HelloSpring"><property name="str" value="Spring"></property></bean></beans>
Another
Package com. pb. demo; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; public class SpringDemo1 {private String name; private String job; public void print () {System. out. println ("name:" + this. getName () + "\ t occupation:" + this. getJob ();} public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext. xml "); SpringDemo1 demo = (SpringDemo1) context. getBean ("springdemo"); demo. print ();} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getJob () {return job;} public void setJob (String job) {this. job = job ;}}
<? 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: p = "http://www.springframework.org/schema/p" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id = "hellospring" class = "com. pb. demo. helloSpring "> <property name =" str "value =" Spring "> </property> </bean > <Bean id = "springdemo" class = "com. pb. demo. springDemo1 "> <property name =" name "value =" Lin Chong "/> <property name =" job "value =" 0.8 million military ban! "/> </Bean> </beans>
6. Spring supports the single-State and factory modes by 6.1.
Package com. pb. lnk;/*** cartridge interface * @ author Administrator **/public interface Lnk {public void getColor ();}
Implementation class
Package com. pb. lnk;/*** Color cartridge * @ author Administrator **/public class Color implements Lnk {@ Override public void getColor () {System. out. println ("======= print with a color cartridge! ======== ") ;}} Package com. pb. lnk;/*** gray ink cartridge * @ author Administrator **/public class Grey implements Lnk {@ Override public void getColor () {System. out. println ("-------- print with gray ink cartridge! ---------");}}
Test class
Package com. pb. lnk; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext;/*** cartridge test class ** @ author Administrator **/public class LnkTest {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext. xml "); Lnk k = null; k = (Lnk) context. getBean ("color"); k. getColor (); k = (Lnk) context. getBean ("gray"); k. getColor ();}}
ApplicationContext. xml
<bean id="color" class="com.pb.lnk.Color"></bean><bean id="grey" class="com.pb.lnk.Grey"></bean>
6.2 single-State mode support
Package com. pb. lnk; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext;/*** single-State mode (single-instance mode) * @ author Administrator **/public class LnkTest2 {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext. xml "); Lnk lnk1 = null; Lnk lnk2 = null; lnk1 = (Lnk) context. getBean ("color"); lnk2 = (Lnk) context. getBean ("color"); System. out. println (lnk1 = lnk2); // The result is true, indicating that 2 are the same object }}VII. Spring Core Mechanism