i creating standalone application that’s based on spring framework & hibernate.
the main method in application class looks this:
public static void main(string[] args) { system.out.println("starting application...."); applicationcontext context = new annotationconfigapplicationcontext(application.class); ingest ingest = context.getbean(ingest.class); ingest.ingest(args[1]); } in ingestionimpl, i've:
@componentscan @component public class ingestimpl implements ingest { private static final logger logger = logmanager.getlogger(ingestimpl.class); @autowired applicationcontext applicationcontext; @autowired private mappingdao mappingdao; where mappingdao looks this:
@component @transactional public interface mappingdao extends crudrepository<mapping, long> { public list<mapping> findbytype(string type); } when run this,
beancreationexception: not autowire field: private com.xxx.mappingdao.
what doing wrong?
you have create configuration class , use @componentscan configuration in class, ex application.class, beans have created in app, see valid example below.
package com.brito.config; import org.springframework.beans.factory.annotation.value; import org.springframework.cache.cachemanager; import org.springframework.cache.annotation.cachingconfigurersupport; import org.springframework.cache.annotation.enablecaching; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.propertysource; import org.springframework.context.support.propertysourcesplaceholderconfigurer; import org.springframework.data.redis.cache.rediscachemanager; import org.springframework.data.redis.connection.redisconnectionfactory; import org.springframework.data.redis.connection.jedis.jedisconnectionfactory; import org.springframework.data.redis.core.redistemplate; @configuration @enablecaching @componentscan("com.brito.service.impl") @propertysource("classpath:/redis.properties") public class cacheconfig extends cachingconfigurersupport { @value("${redis.host-name}") private string redishostname; @value("${redis.port}") private int redisport; @bean public static propertysourcesplaceholderconfigurer propertysourcesplaceholderconfigurer() { return new propertysourcesplaceholderconfigurer(); } @bean public jedisconnectionfactory redisconnectionfactory() { jedisconnectionfactory redisconnectionfactory = new jedisconnectionfactory(); redisconnectionfactory.sethostname(redishostname); redisconnectionfactory.setport(redisport); return redisconnectionfactory; } @bean public redistemplate<string, string> redistemplate(redisconnectionfactory cf) { redistemplate<string, string> redistemplate = new redistemplate<string, string>(); redistemplate.setconnectionfactory(cf); return redistemplate; } @bean public cachemanager cachemanager(redistemplate<string, string> redistemplate) { rediscachemanager cachemanager = new rediscachemanager(redistemplate); // number of seconds before expiration. defaults unlimited (0) cachemanager.setdefaultexpiration(300); return cachemanager; } } package com.brito.service.impl; import org.springframework.cache.annotation.cacheable; import org.springframework.stereotype.service; import com.brito.service.messageservice; @service("messageservice") public class messageserviceimpl implements messageservice { @override @cacheable("messages") public string getmessage(string name) { system.out.println("executing messageserviceimpl" + ".gethellomessage(\"" + name + "\")"); return "hello " + name + "!"; } }
Comments
Post a Comment