Estou com seguinte problema quando tento rodar a classe “Tester” dá o seguinte erro:
28/11/2008 16:08:47 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1bca5f1: display name [org.springframework.context.support.FileSystemXmlApplicationContext@1bca5f1]; startup date [Fri Nov 28 16:08:47 BRST 2008]; root of context hierarchy
28/11/2008 16:08:47 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\diogo_webapps\workspace\SampleBeanImple\beanDefinition.xml]
28/11/2008 16:08:47 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@1bca5f1]: org.springframework.beans.factory.support.DefaultListableBeanFactory@18235ed
28/11/2008 16:08:47 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18235ed: defining beans [sample,interceptor,proxyBean]; root of factory hierarchy
28/11/2008 16:08:47 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18235ed: defining beans [sample,interceptor,proxyBean]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sample' defined in file [C:\diogo_webapps\workspace\SampleBeanImple\beanDefinition.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ISampleBean]: Specified class is an interface
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:883)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:839)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
at com.Tester.loadContext(Tester.java:16)
at com.Tester.main(Tester.java:23)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ISampleBean]: Specified class is an interface
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:51)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:877)
... 17 more
package com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Tester {
private static ApplicationContext context;
/**
* Load up the Spring container
*/
private void loadContext()
{
String filename = "beanDefinition.xml";
context = new FileSystemXmlApplicationContext(filename);
}
public static void main(String[] args)
{
Tester test = new Tester();
test.loadContext();
//reference the proxy through the Spring container
SampleBean sb = (SampleBean)context.getBean("proxyBean");
sb.setName("New Name");
}
}
package com;
public interface ISampleBean{
public void setName(String name);
public String getName();
public void setAge(String age);
public String getAge();
}
SampleBean.java
package com;
public class SampleBean implements ISampleBean {
@Override
public String getAge() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getName() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setAge(String age) {
// TODO Auto-generated method stub
}
@Override
public void setName(String name) {
// TODO Auto-generated method stub
}
}
Parece ser um exemplo simples com interceptor. Nada de especial.
Você aprendeu a iniciar o applicationcontext spring (através de FileSystemXmlApplicationContext) e obter um bean com interceptor através desse applicationcontext (context.getBean(“proxyBean”)).
Após, procure por exemplos de spring com apps web e anotações, fica bacana.