开发者社区> 问答> 正文

JFinal如何配置springPlug?报错


这是service中的代码:

@Service
public class Tuserinfoservice {
 @BY_NAME
 private Tuserinfo tuserinfo;
 public Tuserinfo findbyid(String username){
  return  tuserinfo.findbyid(username);
 }}

这是controller中注解部分的代码:

public class HelloController extends Controller {
 
 @BY_NAME
 private Tuserinfoservice tuserinfoservice;}

这是实体类中的注解部分的代码:

@Repository
public class Tuserinfo extends Model<Tuserinfo> {
 public static final Tuserinfo tuserinfo=new Tuserinfo();}

这是spring的配置文件:

<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:flex="http://www.springframework.org/schema/flex"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
 http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd"
 >
 <!-- bean扫描路径,如果有多个可使用“,”、“;”分隔  -->
 <context:component-scan base-package="com.blogsys"/>
</beans>

项目,用new获取的service可以正常获取数据,可以用注解的service就报错:service空指针。请问这样写哪里错了啊

展开
收起
爱吃鱼的程序员 2020-06-22 11:50:45 578 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    谢谢!已经解决,可以实现自动扫描,注解获取service,entity。

    @Inject么有这个注解符号啊之前做整合也碰到过这个问题,后来没有用Repository注解,直接在xml里面定义了bean就搞定了,具体原因我也没查
    @JFinal大神,帮忙  需要通过IocInterceptor配合@Inject.BY_NAME或@Inject.BY_TYPE对Controller进行注入,被注入的对象是被Spring管理的,所以其后的规则全由Spring决定,JFinal手册有示例代码

    引用来自“缀金玉”的答案

    谢谢!已经解决,可以实现自动扫描,注解获取service,entity。

    亲测可用:假设你的底层框架为:entity(该框架本部分也相当于dao)---service----facade----controller,注解获取实例思路如下

    使用本框架自带的spring插件,先添加该插件,默认会读取applicationcontext.xml文件,具体配置看官方文档。

    1.sprig配置文件中添加自动扫描:

    <context:component-scanbase-package="com.blogsys"/>

    2.service添加注解:@Service。获取entity(dao):

    @Repository

    privateUseruser;

    facade获取service同理。

    3.controller获取facade:controller给注解@Controller

    @By_name

    privateUserFacadeuserFacade;

    这里必须用这个注解才可以获取到facade.service中必须用Repository获取下级实体类。

    其他方案或者问题请赐教。

     

    controller注解:@Before(IocInterceptor.class)
    2020-06-22 11:51:01
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Java Spring Boot开发实战系列课程【第15讲】:Spring Boot 2.0 API与Spring REST Docs实战 立即下载
Java Spring Boot开发实战系列课程【第7讲】:Spring Boot 2.0安全机制与MVC身份验证实战(Java面试题) 立即下载
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题) 立即下载