About Spring
2013-12-27 11:21
253 查看
For simplify Java development:
In 1996, Sun published the JavaBeans 1.0 specification which define a set of coding policies that enable simple Java objects to be reusable and easily composed into more complex application.In 1998, Sun published the Enterprise JavaBeans 1.0 specification which providing much-needed enterprise service, but fail to continue simplicity of the original JavaBeans specification
Today, Java component development has returned to its root. New programming techniques, including DI and AOP are giving JavaBeans much power previously reserved for EJBs. And Spring employs four key strategies for this:
1. Lightweight and minimally invasive development with plain old Java objects(POJOs).
2. Loose coupling through Dependency injection and Interface orientation.
3. Declarative programming through aspects and common conventions.
4. Boilerplate reduction through templates.
About Spring container:
In a Spring-base application, all application objects will live within the Spring container. Spring comes with several container implementations that can be categorized into two distinct types: Bean factories and Application contexts. And there are threeregular Application contexts:
ClassPathXmlApplicationContext -- Loads a context definition from an XML file located in the classpath
FileSystemXmlApplicationContext -- Loads a context definition f
4000
rom an XML file located in the file system
XmlWebApplicationContext -- Loads a context definition from an XML file within a web application
obviously you can manipulate Spring container through the XML files.
About Spring modules:
Aboub wiring beans:
when declaring beans in XML file, there are several namespace(root element) you can choose, but the <beans> is the most important one and it looks like this:<?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-3.0.xsd"> <!-- Bean declarations go here--> </beans>
declaring a simple bean:
package com.myspring.example; public class SimpleBean{ private int i = 1; public void f(){ System.out.println("hello world"); } }
<bean id="simple" class="com.myspring.example.SimpleBean" />
wiring beans for constructor:
<constructor-arg /> element indicate the constructor's arguments, the "value" define base type data and "ref" define reference type data
<bean id="xxx" class="xxx"> <constructor-arg value="1" /> <constructor-arg ref="simple" /> </bean>
wiring beans for method:
"factory-method" attribute indicate a static method, "init-method" specifies a method that is to be called on the bean immediately instantiation and "destroy-method" specifies a method that is called just before a bean is removed from the container
<bean id="xxx" class="xxx" factory-method="staticMethod" /> <bean id="xxx" class="xxx" init-method="afterInsert" /> <bean id="xxx" class="xxx" destroy-method="beforeRemove" />
wiring beans for feild:
<bean id="xxx" class="xxx" > <property name="xxx" value="xxx" /> <!--simple field--> <property name="xxx" ref="xxx" /> <!--object field--> <property name="xxx"> <bean class="xxx" /> </property> <property name="xxx"> <!--List field--> <list> <ref bean="xxx"> ... </list> </property> <property name="xxx"> <!--Map field--> <map> <entry key="xxx" value="xxx"> <entry key-ref="xxx" value-ref="xxx"> ... </map> </property> <property name="xxx"> <!--Map<String> field--> <props> <prop key="xxx">value</prop> ... </props> </property> <property name="xxx"> <!--null value for the field--> <null /> </property> </bean>
wiring with expressions:
Spring 3 introduced the Spring Expression Language(SpEL) which a powerful yet succinct way of wiring, the #{ } markers are a clue to Spring that the content is a SpEL expression
<bean id="xxx" class="xxx" > <property name="xxx" value="#{exp}" /> <!--the basic form--> <property name="xxx" value="#{BeanID}" /> <!--equals ref="BeanID"--> <property name="xxx" value="#{BeanID.field}" /> <!--access the Bean's field--> <property name="xxx" value="#{BeanID.method()}" /> <!--access the Bean's method--> <property name="xxx" value="#{BeanID.methodA()?.methodB()}" /> <!-- ?. indicate a null judgement, if methodA()==null then stop the step--> <property name="xxx" value="#{T(xxx.class).method()}" /> <!--T(Class) indicate a specify Class, generally be used for invoke static method--> <property name="xxx" value="#{exp match 'xxx'}" /> <!--xxx indicate a regular expression--> <property name="xxx" value="#{CollectID[index]}" /> <!--access the value of the collection, index begins with 0--> <property name="xxx" value="#{CollectID[value]}" /> <!--access straightly the value of the collection--> <property name="xxx" value="#{CollectID.?[exp]}" /> <!-- .? for searching the collection with a filter expression then return the result--> <property name="xxx" value="#{CollectID.^[exp]}" /> <!-- .^ for searching the first matching items from a collection--> <property name="xxx" value="#{CollectID.$[exp]}" /> <!-- .$ for searching the last matching items from a collection--> <property name="xxx" value="#{CollectID.![property]}" /> <!-- .! for searching the specify property of collection--> </bean>
About annotation
Spring supports two types of annotation, one is spring's and another comes from JavaEEAnnotation was based on the autowiring, there are four kinds auto wiring: byName/byType/contructor/autodetect(first try constructor, if failure then try byType)
you need to explicit <context:annotation-config> in the XML file before use the annotation in the java source, or instead of <context:component-scan>
annotation within spring
@Autowired(required=true/false) // maybe can auto wire anything(field/method/constructor) @Qualifier("xxx") // narrow the selection of matching beans with the bean's name @Value("SpEL exp") // using expression for injection
to create a custom qualifier annotation, you need to define an annotation that's itself annotated with @Qualifier:
@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Qualifier public @interface MyQualifier{ }
annotation within JavaEE
@Inject // just like @Autowired @Name("xxx") // just like @Qualifier
annotating for autodiscovery
by default, <context:component-scan> looks for four annotation:
@Component and any custom annotation which be annotated with @Component
@Controller
@Repository
@Service
filtering component-scan: <context:include-filter type="" expression=""> / <context:exclude-filter type="" expression="">
there are five type for the filter: annotation/assignable(be seemed as byType)/aspectj/custom/regex
using Java-base configuration(which against with XML-base configuration)
@Configuration // as a clue to Spring that this class will contain one or more Spring bean declarations, just like what the XML file to do @Bean // declare a bean, equals to <bean> tag in the XML file
About aspect
some key words need to know:advice: define the what and when of aspects, Spring aspects can work with five kinds of advice: Before/After/Around/After-returning/After-throwing
join points: a point in the execution of the application where an aspect can be plugged in
pointcuts: define the where of aspects, pointcuts help narrow down the join point advised by an aspect
aspect: the merger of advice and pointcuts--what it does and where and when it does it
introduction: allows to add new methods to existing classes
weaving: the process of applying aspects to a target object to create a new proxied object, the weaving can take place at several points: CompileTime/ClassLoadTime/Runtime
Spring's AOP support:
spring advice is written in Java
spring advice objects at runtime
spring only supports method join points
writing pointcuts
excution(* com.myspring.example.BeCuttedClass.beCuttedMethod(..)) && within(com.myspring.example.*) // narrow the selection scope && bean(BeanId) // designate the bean in the spring containerannotation aspect
you must import the <aop> namespace before use the annotation and define <aop:aspectj-autoproxy> in correspond XML file, or alternately a explicit definition <bean:theAspectClass>
@Aspect // a clue to spring this class is a aspect class public class CutDown{ @Pointcut("excution (* com.myspring.example.BeCuttedClass.beCuttedMethod(..))") public void cut(){}; // a blank method which just only means a mark @Before("cut()") public void beforeStart(){ System.out.println("invoke beforeStart() before cut()"); } @After("cut()") public void afterEnd(){ System.out.println("invoke beforeStart() before cut() return"); } @AfterReturnning("cut()") public void correctEnd(){ System.out.println("invoke correctEnd() after cut() return"); } @AfterThrowing("cut()") public void wrongEnd(){ System.out.println("invoke wrongEnd() when cut() throw exception"); } }annotation introduction
@Aspect public class IntroductionClass{ @DeclareParents( // a clue to spring this need introduction value = "com.myspring.example.BeCuttedClass+", // target classes dafaultImpl = NewInterfaceImpl.class // addition class which implements the interface ) public static NewInterface newInterface; // specify the interface which add the function }
to be continue...
相关文章推荐
- Spring-MVC入门(一):入门实例1
- 我终于忍不住喷一下某些书了,关于Java传引用的XX言论
- java 实现ftp上传下载
- [Leetcode] Remove Duplicates from Sorted Array (Java)
- JAVA常见中文问题的解决方法(转)
- JAVA 利用spring的JavaMailSenderImpl工具包发送邮件
- java项目中定时任务之Quartz的应用
- springSecurity源码分析——DelegatingFilterProxy类的作用
- 项目视频讲解_JavaWeb阶段项目云工厂网上书城
- eclipse实用插件
- JAVA方法传递参数:传值?传引用?
- springSecurity的登录验证
- 打造高性能、高流量Java Web站点的22条建议
- Java中Math类的常用方法
- Myeclipse Working Sets 分类项目
- JAVA基础 浅谈3DES加密解密
- Java/JS获取flash高宽的具体方法
- Java 的JCuda 以及JOGL安装 配置
- Eclipse中快捷键组合
- spring 小结