Анна Харитонова, Jet Infosystems
public interface BookDao {
public void save();
}
@Repository
class BookDaoImpl {
@Override
@Transactional
public void save() {
...
}
public void flush() {
...
}
}
@Service
class BookServiceImpl {
@Autowired
private BookDao bookDao;
@Override
public void strangeAction() {
((BookDaoImpl) bookDao).flush();
}
}
???
@Component
class DataConsumerComponent {
@Autowired
DataProvider dataProvider; //dataProvider.getClass() ???
}
public interface BookService {
public BookInfo getBookInfo(String id);
public void addBook(BookInfo bookInfo);
}
<bean id="booksWebService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="serviceInterface" value="ru.springproxy.BookService"/>
<property name="wsdlDocumentUrl" value="http://localhost:8888/BookServiceEndpoint?WSDL"/>
<property name="namespaceUri" value="http://springproxy/"/>
<property name="serviceName" value="BookService"/>
<property name="portName" value="BookServiceEndpointPort"/>
</bean>
public class MyBookStoreServiceImpl {
@Autowired
BookService bookService;
public void getBookInfo(String id) {
bookService.getBookInfo(id);
}
}
public class SmartLogger {
public Object doLog(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Started for method " + pjp.getSignature().getName());
try {
return pjp.proceed();
} finally {
System.out.println("Ended for method " + pjp.getSignature().getName());
}
}
}
<aop:config>
<aop:aspect id="logger" ref="smartLogger">
<aop:pointcut id="someOperation"
expression="execution(* ru.annaalkh.springproxy.aspect.*.*(..))"/>
<aop:around
pointcut-ref="someOperation"
method="doLog"/>
</aop:aspect>
</aop:config>
catProxy implements Cat,
Flyable, Swimable
CatImpl
FlyableAspect
SwimableAspect
Proxy
@Component
@Scope(value = "singleton")
public class SingletonBean {
@Autowired
private PrototypeBeanInterface prototypeBean;
public void firstCall() {
prototypeBean.makeCall();
}
public void secondCall() {
prototypeBean.makeCall();
}
}
@Component
@Scope(value = "prototype")
public class PrototypeBean implements
PrototypeBeanInterface {
private int id = new Random().nextInt();
@Override
public void makeCall() {
System.out.println("Bean with id " +
+ id + " is called");
}
}
Bean with id -553917404 is called
Bean with id -553917404 is called
@Component
@Scope(value = "singleton")
public class SingletonBean {
@Autowired
private PrototypeBeanInterface prototypeBean;
public void firstCall() {
prototypeBean.makeCall();
}
public void secondCall() {
prototypeBean.makeCall();
}
}
@Component
@Scope(value = "prototype",
proxyMode = ScopedProxyMode.INTERFACES)
public class PrototypeBean implements
PrototypeBeanInterface {
private int id = new Random().nextInt();
@Override
public void makeCall() {
System.out.println("Bean with id " +
+ id + " is called");
}
}
Bean with id 1681508081 is called
Bean with id 241944767 is called
AutoProxyCreator.postProcessAfterInitialization
@Autowired
Cat cat;
@Autowired
@Flying(proxy="jdk")
Cat cat;
class FlyingInvocationhandler implements Invocationhandler {
private Object target;
@Override
public Object invoke(Object proxy, Method method, Object[] args) {
throws Throwable {
System.out.println("It can fly!");
return method.invoke(target, args)
}
}
Invocationhandler handler = new FlyingInvocationhandler(cat)
Cat flyingCat = (Cat) Proxy.newProxyInstance(Cat.class.getClassLoader(),
new Class[] { Cat.class },
handler);
@Autowired
@Flying(proxy="cglib")
Cat cat;
public class NotVerySmartLogger {
public Object doLog(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Some method execution started");
return pjp.proceed();
}
}
Some method execution started
Some method execution started
Internal service message 1
Some method execution started
Internal service message 2
@Repository
public class BookDaoImpl {
@Transactional
public void createNewBook() {
updateMethodInvocationCounter();
...
}
@Transactional
private void updateMethodInvocationCounter() {
...
}
}
@Service
public void Service1 {
public void method1() {
...
}
...
}
@Service
public void Service2 {
public void method1() {
...
}
...
}
@Component
public void Client1 {
@Autowired
Service1 service;
...
}
@Component
public void Client2 {
@Autowired
Service2 service;
...
}
public interface Service {
public void method1;
...
}
@Service
public void Service1 implements Service {
@Override
public void method1() {
...
}
...
}
<aop:aspectj-autoproxy proxy-target-class="true">
<aop:include name="someNotImportantAspect"/>
</aop:aspectj-autoproxy>