I am new to the Spring Framework, so I decided to buy a book ("Spring in Action" 3rd edition). I am currently in the first chapter, which covers the basics - Injection Injection (DI) and Aspect oriented programming (AOP).
I managed to run this code that showed DI.
1 # Interface implemented by the knight
package com.springinaction.knights; public interface Knight { void embarkOnQuest() throws QuestException; }
1 # Class of Knights
package com.springinaction.knights; public class BraveKnight implements Knight { private Quest quest; public BraveKnight(Quest quest) { this.quest = quest;
# 2Quest interface
package com.springinaction.knights; public interface Quest { void embark() throws QuestException; }
# 2Quest Exception
package com.springinaction.knights; public class QuestException extends RuntimeException { private static final long serialVersionUID = 1L; }
# 2Quest type class
package com.springinaction.knights; public class SlayDragonQuest implements Quest { public void embark() throws QuestException { System.out.println("Slaying Dragon!"); } }
# 3Check brave knight DI
package com.springinaction.knights; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class KnightMain { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("knights.xml");
This is the context of the XML application that introduces the quest to the knight
<?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.2.xsd"> <bean id="quest" class="com.springinaction.knights.SlayDragonQuest" /> <bean id="knight" class="com.springinaction.knights.BraveKnight"> <constructor-arg ref="quest" /> </bean> </beans>
Everything works at this point, but when the author wants to show AOP, something is wrong with the XML file. The idea is that the minstrel sings (magazines) before the knight goes on a quest and sings (leaves the game) when the knight returns from the quest.
Class minstrel
package com.springinaction.knights; public class Minstrel { public void singBeforeQuest() {
AOP XML file, I think there is some kind of error here, but I donβt know what ...
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="knight" class="com.springinaction.knights.BraveKnight"> <constructor-arg ref="quest" /> </bean> <bean id="quest" class="com.springinaction.knights.SlayDragonQuest" /> <bean id="minstrel" class="com.springinaction.knights.Minstrel" /> <aop:config> <aop:aspect ref="minstrel"> <aop:pointcut id="embark" expression="execution(* *.embarkOnQuest(..))" /> <aop:before pointcut-ref="embark" method="singBeforeQuest"/> <aop:after pointcut-ref="embark" method="singAfterQuest"/> </aop:aspect> </aop:config> </beans>
Testing looks exactly the same as in # 3 BraveKnight Testing
An exception that is a long time.
sie 20, 2013 3:06:58 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org .springframework.context.support.ClassPathXmlApplicationContext@ 14d5bc9: startup date [Tue Aug 20 15:06:58 CEST 2013]; root of context hierarchy sie 20, 2013 3:06:58 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [knights-aop.xml] sie 20, 2013 3:06:58 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.s pringframework.beans.factory.support.DefaultListableBeanFactory@ 46aa61: defining beans [knight,quest,minstrel,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor