Autowire annotation is not working for different package - spring

I am using spring3,struts2 and hibernate3 together.I have a samplemanager.java file in com.top package and another test.java file in net.top package.In this test.java file i tried to use the samplemanager.java using #Autowired.But it throws null pointer exception can any one tell me a solution for this.

You have to configure the component scan in your application context file for both packages:
<context:component-scan base-package="com.top.*, net.top.*" />

Check if your samplemanager class is annotated and if you scan the classpath for annotated beans in your configuration file. If it is not annotated you must define the bean in the configuration file.

Related

Parameter 0 of constructor in controller required a bean of type repository class from another package

although i have added the package containing my GemDaoImpl repository class in pom.xml, it is giving error:
Parameter 0 of constructor in com.bfm.accounting.portfolio.controller.PortfolioApiController required a bean of type 'com.bfm.app.reporting.gem.dao.impl.GemDaoImpl' that could not be found.
Action:
Consider defining a bean of type 'com.bfm.app.reporting.gem.dao.impl.GemDaoImpl' in your configuration.
Can you please help me to resolve this error.
try mentioning the package that contains the GemDaoImpl in component-scan element in XML
<context:component-scan base-package="com.mkyong.customer" />
or in case if you are using class-based configuration the use
#ComponentScan(basePackages = {"list of packages"})

#Import fails to aggregate annotated config from dependent jar

I have a project setup where a common module (JPA.jar) containing Spring JPA configuration.
#Configuration
#EnableJpaRepositories({"com.db.jpa.repository"})
#EnableTransactionManagement
public class Jpa {
// ...
}
I intend to invoke the config from a webservice (spring boot) and have a config importing the JPA configuration from JPA.jar.
#Configuration
#Import(com.db.config.Jpa.class)
public class JpaApp {
}
This fails with following error:
Caused by: java.io.FileNotFoundException: class path resource [com/db/config/Jpa.class] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:51)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103)
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:88)
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:75)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:81)
at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:731)
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getRelated(ConfigurationClassParser.java:1007)
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getAnnotationAttributes(ConfigurationClassParser.java:988)
at org.springframework.context.annotation.ConfigurationClassParser.collectImports(ConfigurationClassParser.java:536)
at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:509)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:300)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245)
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:635)
... 40 more
Unable to find any documentation that says this is illegal for Spring's #Import. However I see this is done for resources with #ImportResource, using a classpath prefix.
I can include a set of configs for each webservice component using the common JPA models and repos, but just wondering if aggregating #Configuration(s) specifically using #Import from dependency jars is possible.
Is it possible?
If illegal, is there any rationale to it.
Thanks in advance.
You can use #Import for configuration classes from other jars. I think that you are getting this error because your jar is not defined as dependency in your pom.xml (if you use maven of course), that's why Spring can't find it.

In Spring, can I autowire a property value simply by including that JAR in my WAR file?

I’m using Spring 3.2.11.RELEASE. I have a JAR file with a class that has the following
#Service(“myService")
public class MyServiceImpl implements MyService
{
…
#Value(“#{myProperties[‘my.properties.key’]}”)
private String myPropertiesValue;
When I include the JAR file in a WAR, I must put this in the WAR’s application context or else the autowiring fails …
<util:properties id=“myProperties" location="classpath:my_file.properties" />
The file “my_file.properties” is at the root of my JAR file. My question is, is there any way I can get the autowiring of the property to occur by simply including my JAR file in my WAR? I realize that adding ‘<util:properties id=“myProperties" location="classpath:my_file.properties" />’ is not that hard, but when I include the above JAR in dozens of projects, it is easy to forget to include the “util” declaration in one or two, causing those applications to fail to deploy.
There is no other conceptually different way. Either you include the properties or you can import full xml which defines part of the application context specific to this jar:
<import resource="classpath:my_jar_config.xml" />
where my_jar_config.xml contains all beans and the <util:properties> specific to this jar.

hibernate-cfg.xml and property placeholders

I create my hibernate-cfg.xml file using the Ant hibernatecfg task in my build file. I reference that file in my sessionFactory bean (org.springframework.orm.hibernate3.LocalSessionFactoryBean) in the configLocation property (classpath:hibernate.cfg.xml).
In my bean definitions file, I also have a propertyConfigurer (org.springframework.beans.factory.config.PropertyPlaceholderConfigurer).
Will the propertyConfigurer do substitutions in the hibernate.cfg.xml file or do I have to hard-code everything in that file?
Spring won't resolve properties in hibernate.cfg.xml.

Spring load parts of one config with different loaders from file and classpath

Is it possible load custom part of config from file and automatically load other part of config from classpath, placed inside jar?
I have command line apllication written on java with spring 2.5.6 framework.
Config of apllication consist from 2 parts:
bigApplicationContext.xml
customConfig.xml with import of bigApplicationContext.xml
bigApplicationContext has references to some beans from customConfig.
I placed bigApplicationContext and default customConfig inside jar. Configs loaded by ClassPathXmlApplicationContext. It's OK
Troubles goes when I want provide additional command line option for my application --pathToCustomConfig
I want to load custom part of config from file and automatically load other part of config from bigApplicationContext, placed inside jar.
Is it possible? Now I have
Configuration problem: Failed to import bean definitions from relative
location
Found solution with using
<import resource="classpath:applicationContext.xml" />
I used
<import resource="applicationContext.xml" />

Resources