It seems that I searched for information from various sources over google to solve my problem, but I am still in trouble.
To clarify my problem I will give you as much information as I can.
I am using NetBeans IDE 7.3.1 and Spring 3.1.1.
What I have now:
I created 6 classes and 1 xml file.
These classes are working fine (because these classes are based from book which I am currently reading).
I suppose something is wrong with my jar files (maybe somthing is wrong with versions compatibility or something (idk, i am new in this scope)).
XML FILE:
<?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.0.xsd
**http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">**
<bean id="knight" class="castle.BraveKnight">
<constructor-arg ref = "quest"/>
</bean>
<bean id="quest"
class="castle.SlayedDragonQuest"/>
<bean id="mistreal"
class="castle.Mistreal"/>
<aop:config>
<aop:aspect ref="mistreal">
<aop:pointcut id="embark"
expression="execution(* *.embarkQuest(..))" />
<aop:before pointcut-ref="embark"
method="singBeforeQuest"/>
<aop:after pointcut-ref="embark"
method="signAfterQuest"/>
</aop:aspect>
</aop:config>
</beans>
After compiling my code, I am getting this:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [knight.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice
It is obvious that classloader cannot find my aop class which is called Advice. But why?
I added jars to my project like this:
Did I miss something? Can anyone give me some information. (I read related resources but result is the same).
Thank you
EDITED:
I added aopalliance.jar file, now I am getting this:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'knight' defined in class path resource [knight.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#1': Cannot create inner bean '(inner bean)' of type [org.springframework.aop.aspectj.AspectJAfterAdvice] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Cannot create inner bean '(inner bean)' of type [org.springframework.aop.config.MethodLocatingFactoryBean] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Unable to locate method [signAfterQuest] on bean [mistreal]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:452)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
It seems that one solution rises another problem. How can I fix it?
P.s. I am not using a Marven. And also this is a mistake in my picture, i fixed it in my project, but it didn't change anything.
Mistreal class:
package castle;
public class Mistreal {
public void singBeforeQuest(){
System.out.println("Singing before quest");
}
public void singAfterQuest(){
System.out.println("Singing after quest");
}
}
Yes, you need the aopalliance library which contains, among others, the org.aopalliance.aop.Advice class. You can get it for maven (or download the jar), here.
If you are using Maven, then spring-aop should bring it in, since it is one of its dependencies. If not, you will have to download it (and anything else) and add it (them) manually to your classpath/build path.
As others have noted, use a single spring-aop jar.
You have a typo
<aop:after pointcut-ref="embark"
method="signAfterQuest"/>
In the above pointcut, you use signAfterQuest but your method is called
public void singAfterQuest(){
System.out.println("Singing after quest");
}
singAfterQuest. The hint is in the logs
Unable to locate method [signAfterQuest] on bean [mistreal] at
For the new error, Spring, by default, uses JDK proxies, but those only work on interfaces. You therefore cannot inject a Proxy into a variable of a class type. Instead you will need to use CGLIB proxies with this change to your XML configuration
<aop:config proxy-target-class="true">
This will also require you add CGLIB and related libraries to your classpath. More details in related answers like here.
I notice that you have both:
spring-aop-3.0.7.RELEASE.jar and spring-aop-3.0.0.RELEASE.jar
Is this intended? Maybe there is some sort of conflict.
Edit: as #SotiriosDelimanolis pointed out, you need the aopalliance jar located here. Maven is definitely a good resource for pulling in required dependencies, and easy to use. I would recommend looking into it.
Related
I'm trying to instantiate a bean, without a class definition, on Spring context by inheriting a parent bean, which is abstract. Like this:
<bean id="childBean" parent="abstractBean">
...
</bean>
<bean id="abstractBean" class="com.java.bean.AbstractBeanClass"
abstract="true" />
But Spring is giving me the following error message:
Error creating bean with name 'childBean' defined in class path resource
[project-spring.xml]:
Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [com.java.bean.AbstractBeanClass]:
Is it an abstract class?;
nested exception is java.lang.InstantiationException
...
I remember having this in another project, and it works perfectly.
I remember having this in the same project, and it works perfectly.
What am I missing here?
UPDATE 1
Found another bean being intatiated exactly like the way I mentioned:
<bean id="variantOptionDataConverter" parent="abstractPopulatingConverter">
...
</bean>
UPDATE 2
Declaration of the Abstract class:
public abstract class AbstractBeanClass<SOURCE, TARGET>
extends AbstractConverter<SOURCE, TARGET>
implements PopulatorList<SOURCE, TARGET>
{
...
}
Note: There's other classes that extends this class. None of the "other spring beans that works" I mentioned above extends this class.
Note 2: I know that it's weird, and, by Java fundamentals, it should not work (like everyone mentioned). But, I don't know how, the other Spring beans are being put in the context. I tried to copy the "other spring beans that work" into my code, change some duplicated names, and it worked. And, that's what I'm trying to understand...
This concrete example Juliano is referring to is from the hybris commerce suite.
Hybris has the concept of extensions that have dependencies on each other. Each extension defines their own application context which has access to its inherited application contexts (from those extensions that are declared as dependencies).
In this example there is an abstractPopulatingConverter bean declaration declared at the root application context where its declared java class is abstract.
However, in other application context further down the dependency tree there is an alias definition for this "abstractPopulatingConverter" that points to a bean definitions with a concrete class.
That's why in a lot of cases (namely the ones that are even further down the dependency tree) using the parent="abstractPopulatingConverter" works as it points to the new bean defined by the alias.
This is the very common message when Spring container faces with absatract Java class (not abstract bean). Check your com.java.bean.AbstractBeanClass code and probably you can find "public abstract class".
What am I missing here?
I don't know what's going on in your other project, but Spring cannot instantiate beans for classes that are abstract.
Concerning abstract beans, the Spring documentation states
A child bean definition inherits configuration data from a parent definition.
[...]
A child bean definition uses the bean class from the parent definition
if none is specified, but can also override it. In the latter case,
the child bean class must be compatible with the parent, that is, it
must accept the parent’s property values.
In other words, an bean definition declared with abstract="true" is simply used as a template for other beans.
Java does not allow instantiation of abstract classes. Spring cannot overrule this language feature. Spring cannot instantiate beans of abstract class types.
Found the answer:
Indeed, like Sotirios Delimanolis commented, "You must have something else going on, proxying of some kind maybe", Spring has a proxy that instantiate this type of bean declaration, but it can't instantiate the bean without its methods (which are just declared in the abstract class, not implemented).
So you must "implement" these methods, like this:
<bean id="childBean" parent="abstractBean">
<lookup-method name="notImplementedMethod" bean="anotherBean"/>
</bean>
The anotherBean must be the same type as notImplementedMethod.
I'm new in spring Framework. And actually i was doing an experiment with spring actually.
Look at this HelloWorld.java:
public class HelloWorld {
private String messageee;
public void setMessage(String messageee){
this.messageee=messageee;
}
public void show(){
System.out.println("message: "+messageee);
}
}
You see in this program, I've one variable which is outside declared as private named as messageee and next variable which is parametrized with setter named as messageee. You see both have same name.
Okay.. Now look at this bean file:
<?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="helloWorld" class="com.springframework.HelloWorld">
<property name="message" value="Hello.. This is Spring Framework example."></property>
</bean>
</beans>
Here you see inside bean tag. I've declared the property name as message. I don't understand, when i give the name as messageee it gives an error as:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorld' defined in class path resource [beans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'messageee' of bean class [com.springframework.HelloWorld]: Bean property 'messageee' is not writable or has an invalid setter method. Did you mean 'message'?
But when i give the name as message. It runs successfully. But i don't have any message's method or any kind of variables with this similar name. So, How setter works actually? Will you please elaborate?
Help would be appreciated!
You're confusing fields (or instance variables) with properties.
property is a term coming from the Java Beans specification. A property foo of a bean is data that can be accessed using a getter method called getFoo() (or isFoo() for a boolean) and/or set using a setter method called setFoo().
What these methods do internally, whether they get/set a variable or not, whether the variable is also named foo or anything else, is completely irrelevant. What matters is the name of the getter/setter.
So, when you define your bean and tell Spring to set the property named message, Spring will look for a method called setMessage(). It doesn't care about the private fields of your bean class.
The Spring IoC container also supports setter injection, which is the preferred method of dependency injection in Spring. Setter injection uses the set* methods in a class file to garner property names that are configurable in the spring XML config.
From a configuration standpoint, setter injection is easier to read because the property name being set is assigned as an attribute to the bean, along with the value being injected.
To determine the property names, Spring follows the JavaBeans Specification.
First of all, you are mixing up fields with properties - also your property name in the applicationContext.xml is wrong (it should be messageee)
You need to use #Autowired annotation with either:
1) fields i.e. messageee
or
2) setter i.e. setMessage()
If you are thinking "what is that!!???" Read about Spring's basic features with beans and how Spring is capable of taking POJOs (Plain Old Java Objects) to and configure them using the IoC framework. Read about #Autowired here - How does autowiring work in Spring?
Then you should be fine with this:
<bean id="helloWorld" class="com.springframework.HelloWorld">
<property name="message" value="Hello.. This is Spring Framework example."></property>
</bean>
BTW...good approach for looking into Spring by using the very basic Java stuff....good luck!
I read on a few websites that with Spring its possible to do setter based Dependency Injection without having to create a setter for the injected variable it. Would nicely tidy up the code. I read this on another site and also here on stackoverflow.
I've tried it but in my case it does not work. I'm using 3.2.0.RELEASE. I'm getting the following error.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DI_without_setter' defined in class path resource [SpringBeans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'url' of bean class [net.comsys.springpropstest.DiWithoutSetter]: Bean property 'url' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
my Java test code
package net.xxx.springpropstest;
public class DiWithoutSetter {
private String url;
}
I've just added it to my main code. Not displayed here. I don't even use DiWithoutSetter in the main code.
SpringBeans.xml
<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.0.xsd">
<bean id="DI_without_setter" class="net.xxx.springpropstest.DiWithoutSetter">
<property name="url" value="jan" />
</bean>
If someone could shed some light on his issue that would be appreciated. Is it possible to set the value of a (public) variable in a Spring bean without using a setter method?
You can't do that (you need a setter), and that tutorial appears to be wrong (note in the example source zip, the classes have getters and setters).
Either add a setter or #Inject the field implicitly using autowiring.
You would need to annotate the field with #Autowired and enable component scanning for this injection to work without a setter method.
I need to load the cron expression for #scheduled annotation dynamically from a property file.
I know the solution for this would be using property place holder, but my problem is that, property file which contains the cron expression can be modified dynamically and its location is derived from another property file.
application-context.xml looks something like this,
<util:properties id="dynamicProps" location="${configurable.property.file.path}"
<context:property-placeholder properties-ref="dynamicProps" />
configurable.property.file.path property is present in a property file that is located under WEB-INF/classes and the value of this is something like this,
configurable.property.file.path=${static.assets.path}/config/configuration.properties
With the above settings on start up server cribs with the following error,
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#1': Cannot resolve reference to bean 'dynamicProps' while setting bean property 'properties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dynamicProps': Invocation of init method failed; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/${configurable.property.file}]
I am guessing the reason for this may be that property files are not yet visible at this point. What is the solution for this? Could someone pl help me on this?
Due to cross-platform constraints, I'm trying to externalize the filter chain in spring security. While it works great when I specify the filter list as a string literal, if I try to use the PropertyPlaceHolderConfigurer to move the filters string out into a properties file, using this code:
<security:filter-chain pattern="/**/*auth=kerberos*"
filters="${kerberosFilters}"/>
I receive the following error message:
Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource
[config/common/security/spring-security.xml]:
Cannot resolve reference to bean 'httpSessionContextIntegrationFilter,logoutFilter,spnegoAuthenticationProcessingFilter,securityContextHolderAwareRequestFilter,spnegoExceptionTranslationFilter,filterSecurityInterceptor'
while setting bean property 'filterChainMap' with key [/**/*auth=kerberos*] with key [0];
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No bean named
'httpSessionContextIntegrationFilter,logoutFilter,spnegoAuthenticationProcessingFilter,securityContextHolderAwareRequestFilter,spnegoExceptionTranslationFilter,filterSecurityInterceptor'
is defined
It looks like when read from a properties file, Spring is trying to treat the entire string as a single bean name.
Is there any way to get around this, or is it a limitation of the Spring framework?
I wonder if something like this would work? (This is untested.)
<property name="filterList">
<list ref="${kerberosFilters}"/>
</property>