java spring

 spring 用來使程序解耦,較重要的有aop(切面編程)和ioc(反向注入)

 ioc用來使數據、對象和業務程序 解耦,即在什麼地方需要一個對象,你自己不用去通過new 生成你需要的對象,而是通過spring的bean工廠爲注入這樣一個對象

 aop用來使業務邏輯和通常可複用的其他交叉邏輯(安全,事物,日誌) 解耦,即將交叉邏輯(比如安全,日誌,事務等),封裝成一個切面,然後注入到目標對象。

for example:

hello_ioc.java:

  package org.dao.impl;

public class hello_ioc {

private String hello_ioc = null;

public String getHello_ioc() {

return hello_ioc;

}

public void setHello_ioc(String hello_ioc) {

this.hello_ioc = hello_ioc;

}

}

Hello.java:
 
package org.dao.impl;
 
public class Hello implements IHello {
 
@Override
public void hello(String hello) {
// TODO Auto-generated method stub
System.out.println("Hello:" + hello);  
}
}
 
IHello.java:
package org.dao.impl;
public interface IHello {
public void hello(String hello);
/*public void sethello(String haha);
public String gethello();*/
}
 
LogBeforeMethod.java:
package org.log;
 
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
 
import org.springframework.aop.MethodBeforeAdvice;
 
public class LogBeforeMethod implements MethodBeforeAdvice {
 
private Logger logger = Logger.getLogger(this.getClass().getName());  
   
@Override
public void before(Method method, Object[] arg1, Object arg2)
throws Throwable {
// TODO Auto-generated method stub
logger.log(Level.INFO, "method start..." + method);  
       System.out.println("LogBeforeAdvice: method start... " + method);  
}
}
 
 
TestBefore.java:
package org.spring.test;
 
import org.dao.impl.IHello;
import org.dao.impl.hello_ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestBefore {
 
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");  
        //獲取的是Hello的代理對象  
//for aop
        IHello helloProxy = (IHello) ctx.getBean("hello");  
        helloProxy.hello("kkf");
        
        //for ioc
        hello_ioc helloioc = (hello_ioc)ctx.getBean("hello_ioc");
        System.out.println(helloioc.getHello_ioc());
}
 
}
 
applicationContext.xml:
 
<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id = "Hello1" class="org.dao.impl.Hello"></bean>
<bean id="logBefore" class="org.log.LogBeforeMethod"></bean>
 
<!-- aop -->
<bean id="hello" class="org.springframework.aop.framework.ProxyFactoryBean"> 
<property name="proxyInterfaces" value="org.dao.impl.IHello"></property>  
    <!-- 代理對象 -->  
    <property name="target" ref="Hello1"></property> 
    
   <!-- <property name ="hehe"><value>kkongyang</value></property>  --> 
    <!-- 攔截器 -->  
    <property name="interceptorNames">  
        <list>  
            <value>logBefore</value>  
        </list>  
    </property>  
</bean>
 
<!-- ioc -->
<bean id="hello_ioc" class="org.dao.impl.hello_ioc">
<property name ="hello_ioc"><value>kkfkkf</value></property>
</bean>
</beans>
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章