Java Tutorial/Spring/XML Bean — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 15:22, 31 мая 2010
Содержание
- 1 After Returning Advice Demo
- 2 Alias Bean Demo
- 3 Annotated Autowiring
- 4 Annotation Component
- 5 Autowiring
- 6 BeanName Aware
- 7 Factory Bean Demo
- 8 Factory Object Integration
- 9 Get Method By Name
- 10 Inheritance Demo
- 11 Local Reference
- 12 Method Loopup
- 13 Required Property Not Set Exception
- 14 Spring factory method
- 15 XML Based Bean Configuration
- 16 XML Bean Injection with namespace
After Returning Advice Demo
File: Main.java
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import bean.MyClass;
import bean.SimpleAfterAdvice;
public class Main {
public static void main(String[] args) {
MyClass target = new MyClass();
AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
pc.setExpression("execution(* bean..*.get*(..))");
Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleAfterAdvice());
ProxyFactory pf = new ProxyFactory();
pf.setTarget(target);
pf.addAdvisor(advisor);
MyClass proxy = (MyClass) pf.getProxy();
System.out.println(proxy.getName());
proxy.setName("New Name");
System.out.println(proxy.getHeight());
}
}
Alias Bean Demo
File: context.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="name1" name="name2,name3,name4" class="java.lang.String"/>
<alias name="name1" alias="namex1"/>
<alias name="name1" alias="namex2"/>
</beans>
Annotated Autowiring
File: context.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="a" class="A"/>
<bean id="b" class="B"/>
<bean id="annotatedTarget" class="AnnotatedClass"/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>
Annotation Component
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy />
<context:component-scan base-package="bean">
<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
</beans>
Autowiring
File: context.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="a" class="A"/>
<bean id="b" class="B"/>
<bean id="byName" autowire="byName" class="MyClass"/>
<bean id="byType" autowire="byType" class="MyClass"/>
<bean id="constructor" autowire="constructor" class="MyClass"/>
<bean id="autodetect" autowire="autodetect" class="MyClass"/>
</beans>
BeanName Aware
File: context.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="logging" class="LoggingBean"/>
</beans>
Factory Bean Demo
File: context.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sha" class="MessageDigestFactoryBean">
<property name="algorithm" value="SHA1"/>
</bean>
<bean id="md5" class="MessageDigestFactoryBean"/>
</beans>
Factory Object Integration
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--(1) -->
<bean id="socketFactory" class="javax.net.SocketFactory" factory-method="getDefault">
</bean>
<bean id="localhost"
factory-bean="socketFactory" factory-method="createSocket">
<constructor-arg value="localhost"/>
<constructor-arg value="80"/>
</bean>
<bean id="jexp.ru"
factory-bean="socketFactory" factory-method="createSocket">
<constructor-arg value="www.jexp.ru"/>
<constructor-arg value="80"/>
</bean>
</beans>
Get Method By Name
File: Main.java
import java.lang.reflect.Method;
import org.springframework.aop.Advisor;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.ruposablePointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcher;
public class Main {
public static void main(String[] args) {
SampleBean target = new SampleBean();
ComposablePointcut pc = new ComposablePointcut(ClassFilter.TRUE, new GetterMethodMatcher());
pc.intersection(new GetAgeMethodMatcher());
SampleBean proxy = getProxy(pc, target);
testInvoke(proxy);
}
private static SampleBean getProxy(ComposablePointcut pc, SampleBean target) {
Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleBeforeAdvice());
ProxyFactory pf = new ProxyFactory();
pf.setTarget(target);
pf.addAdvisor(advisor);
return (SampleBean) pf.getProxy();
}
private static void testInvoke(SampleBean proxy) {
proxy.getAge();
proxy.getName();
proxy.setName("QQQ");
}
}
class GetterMethodMatcher extends StaticMethodMatcher {
public boolean matches(Method method, Class cls) {
return (method.getName().startsWith("get"));
}
}
class GetAgeMethodMatcher extends StaticMethodMatcher {
public boolean matches(Method method, Class cls) {
return "getAge".equals(method.getName());
}
}
class SampleBean {
public String getName() {
return "AAA";
}
public void setName(String name) {
}
public int getAge() {
return 100;
}
}
class SimpleBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("Before method " + method);
}
}
Inheritance Demo
File: context.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="parent" class="SimpleBean" abstract="true">
<property name="name" value="Name 1"/>
</bean>
<bean id="bean1" class="SimpleBean" parent="parent">
<property name="age" value="28"/>
</bean>
<bean id="bean2" class="SimpleBean" parent="parent"/>
</beans>
Local Reference
File: context.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="weatherService" class="WeatherServiceImpl">
<property name="weatherDao">
<ref local="weatherDao"/>
</property>
</bean>
<bean id="weatherDao" class="StaticDataWeatherDaoImpl">
</bean>
</beans>
Method Loopup
File: context.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="dataService" class="DataServiceImpl">
<lookup-method name="getDataDao" bean="dataDao"/>
</bean>
<bean id="dataDao" singleton="false"
class="StatefulDataDataDaoImpl">
</bean>
</beans>
Required Property Not Set Exception
File: context.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="nameClass"
class="InitializingBeanNameClass"/>
</beans>
Spring factory method
File: context.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="pattern" class="java.util.regex.Pattern" factory-method="compile">
<constructor-arg value="abc"/>
</bean>
</beans>
XML Based Bean Configuration
File: Main.java
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
public static void main(String[] a) {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("context.xml"));
Oracle oracle = (Oracle) bf.getBean("oracle");
System.out.println("Meaning of life is " + oracle.defineMeaningOfLife());
}
}
interface Oracle {
String defineMeaningOfLife();
}
interface Encyclopedia {
Long findLong(String entry);
}
class BookwormOracle implements Oracle {
private Encyclopedia encyclopedia;
public String defineMeaningOfLife() {
Long ageOfUniverse = this.encyclopedia.findLong("A");
Long constantOfLife = this.encyclopedia.findLong("C");
return String.valueOf(ageOfUniverse / constantOfLife);
}
public void setEncyclopedia(Encyclopedia encyclopedia) {
this.encyclopedia = encyclopedia;
}
}
class HardcodedEncyclopedia implements Encyclopedia {
private Map<String, Long> entryValues = new HashMap<String, Long>();
public HardcodedEncyclopedia() {
this.entryValues.put("A", 1L);
this.entryValues.put("C", 3L);
}
public Long findLong(String entry) {
return this.entryValues.get(entry);
}
}
XML Bean Injection with namespace
File: Main.java
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(
"context.xml"));
InjectSimpleDemo simple = (InjectSimpleDemo) factory.getBean("injectSimpleDemo");
System.out.println(simple);
}
}
class InjectSimpleDemo {
private String name;
private int age;
private float height;
private boolean isProgrammer;
private Long ageInSeconds;
public void setAgeInSeconds(Long ageInSeconds) {
this.ageInSeconds = ageInSeconds;
}
public void setIsProgrammer(boolean isProgrammer) {
this.isProgrammer = isProgrammer;
}
public void setAge(int age) {
this.age = age;
}
public void setHeight(float height) {
this.height = height;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return String.format("Name: %s\nAge: %d\nAge in Seconds: %d\nHeight: %g\nIs Programmer?: %b",
this.name, this.age, this.ageInSeconds, this.height, this.isProgrammer);
}
}