Spring, Annotation Based Configuration Example. Compiling and Running from Terminal.


Ah this simple ones..

I made a few modifications on what I had here.

I had to add the .jar file: spring-aop-4.2.5 to make things work.

beans.xml changed quite a bit:
<?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:context="http://www.springframework.org/schema/context"
       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-4.0.xsd">
    <context:component-scan base-package="biz.tugay.learnspring"/>
</beans>

And 2 classes, namely ServiceImpl and ServiceConsumerImpl changed:
package biz.tugay.learnspring;
 
import org.springframework.stereotype.Component;
 
@Component("service")
public class ServiceImpl implements Service{
 public int getServiceNumber(){
  return 5;
 }
}
 
package biz.tugay.learnspring;
 
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
 
@Component("serviceConsumer")
public class ServiceConsumerImpl implements ServiceConsumer{
 
 @Autowired
 private Service service;
 
 public int consumeService(){
  return service.getServiceNumber();
 }
 
 @Autowired
 public void setService(Service service){
  this.service = service;
 }
}

And lets check if everything is still fine:
Korays-MacBook-Pro:learnspring koraytugay$ java -cp ./lib/spring-aop-4.2.5.jar:./lib/spring-expression-4.2.5.jar:./lib/commons-logging-1.2.jar:./:./lib/spring-core-4.2.5.jar:./lib/spring-context-4.2.5.jar:./lib/spring-beans-4.2.5.jar:./target biz.tugay.learnspring.TestClass
Mar 04, 2016 9:58:20 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1d56ce6a: startup date [Fri Mar 04 21:58:20 EET 2016]; root of context hierarchy
Mar 04, 2016 9:58:20 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
5

Allright!!!

At this point, I would like to paste here the documentation for component-scan tag, found in the http://www.springframework.org/schema/context namespace:
Tag name: component-scan
Description: Scans the classpath for annotated components that will be auto-registered as Spring beans.
By default, the Spring-provided @Component, @Repository, @Service, and @Controller stereotypes will be detected.
Note: This tag implies the effects of the 'annotation-config' tag, activating @Required, @Autowired, @PostConstruct, @PreDestroy, @Resource, @PersistenceContext and @PersistenceUnit annotations in the component classes, which is usually desired for autodetected components (without external configuration).
Turn off the 'annotation-config' attribute to deactivate this default behavior.