java - How to Spring Inject My Controller layer from Groovy -
i'm trying learn groovy , integrating existing java jar. java code makes use of di cant seem work groovy script.
the java application contains data access layer using mybatis. layer consists of number of interfaces (e.g iuser) , controllers e.g.
@service public class usercontroller implements iuser
the controllers make use of mybatis mapper classes.
the whole thing pulled using spring default-autowire="byname">
its set use annotations access mappers within controllers.
mybatis configured in spring scan , inject mappers
<bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="com.directski.data.mapper" /> <property name="sqlsessionfactory" ref="sqlsessionfactory"></property> </bean>
so when run application in java works normally. including mappers have called using
@autowired private usermapper usermapper;
when try include jar part of groovy scrip start run problems. im using same applicationcontext file spring
applicationcontext ctx = new classpathxmlapplicationcontext("controller-layer-applicationcontext.xml");
when run script can see logs components scanned. of controllers include @postconstruct method gets called, , database queries sucessfully executed. when trying call controllers script null pointer errors.
i have tried using @autowired create controllers in groovy dont seem injected. have implemented factory.registerbeandefinition() per examples in http://groovy.codehaus.org/using+spring+factories+with+groovy seem controller created, mybatis mappers within controllers returning null
how can make sure controllers autowired correctly groovy?
ok, have thats working. taken answer 5 how programmatically create bean definition injected properties?
public class appcontextextendingbean implements applicationcontextaware{ @override public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception{ autowirecapablebeanfactory beanfactory = applicationcontext.getautowirecapablebeanfactory(); version1(beanfactory); } private void version1(autowirecapablebeanfactory beanfactory){ iuser usercontroller= (usercontroller) beanfactory.createbean(usercontroller,autowirecapablebeanfactory.autowire_by_type, true); beanfactory.initializebean(usercontroller, "user"); user testuser = usercontroller.getuser(3); } } applicationcontext ctx = new classpathxmlapplicationcontext("controller-layer-applicationcontext.xml"); appcontextextendingbean bean = new appcontextextendingbean(); bean.setapplicationcontext(ctx);
Comments
Post a Comment