cirton
V2EX  ›  Java

关于 springmvc 的两个基础问题?

  •  
  •   cirton · Mar 26, 2019 · 2444 views
    This topic created in 2608 days ago, the information mentioned may be changed or developed.

    刚接触 springmvc 框架,有几个问题不太明白:

    1. 如果不需要跟前台页面交互,数据由后台直接生成然后插入数据库,是不是就不需要 controller 层了? 直接在 service 层将数据进行写入?

    2. service 或者 controller 类中引入多个类,是不是每个类都要加注解?如下:

    @Controller
    public class UserController{
    	@Resource
    	private UserService userService;
    	
    	@Resource
    	private User User;
    	
        //DataInfo 类 是否需要注解?
    	private DataInfo  dataInfo = new DataInfo(Type.User.getValue(), "User");
    	
    	 
    	public void addUser(User User) {
    		parseJson();
    		userService.insertUser(User);
    	}
      .......
      .......
     
    }
    
    5 replies    2019-03-28 16:06:41 +08:00
    Resource
        1
    Resource  
       Mar 26, 2019
    1,不需要
    2,不需要
    puyorik
        2
    puyorik  
       Mar 26, 2019
    是的,这和 springmvc 就没关系了,其次不要用 jsr250 的注解
    leafre
        3
    leafre  
       Mar 26, 2019
    后台直接生成然后插入数据库,为何还要用 MVC ?
    cirton
        4
    cirton  
    OP
       Mar 27, 2019
    @puyorik jsr250 注解,是指 @Resource 吗?这个不好用吗?还请指教。我是看教学视频上这样用的。
    anzu
        5
    anzu  
       Mar 28, 2019
    关于你举例的那个 DataInfo 类,如果确认 DataInfo 中没有使用任何注解来注入实例,可以这样用。如果 DataInfo 中还使用了诸如 Resource、Autowired 之类的注解,那么 DataInfo 中的这些注解无效,相关对象为 NULL。不注意就会给自己挖坑。
    https://www.cnblogs.com/chyu/p/4655475.html
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2782 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 11:32 · PVG 19:32 · LAX 04:32 · JFK 07:32
    ♥ Do have faith in what you're doing.