Spring Project没有创建bean

时间:2018-02-03 15:12:52

标签: java spring spring-mvc design-patterns dependency-injection

我正在尝试开发一个Spring MVC项目,但在tomcat上运行之后,我在点击后获得了404:http://localhost:8080/magicart-service/product 我在Controller bean中注入静态代码以查看它是否已创建,但我在控制台中看不到任何输出。这是我所有的课程:

AppConfig.class:

import random
from collections import Counter

prob = Counter(random.randint(1,6) for _ in range(1000000))

print(prob)
>> Counter({1: 166503, 2: 166833, 3: 166531, 4: 166681, 5: 166846, 6: 166606})

AppInitializer.java

package com.magicart.poc.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.magicart.poc")
public class AppConfig {

}

ProductController.java

package com.magicart.poc.config;
import org.springframework.core.annotation.Order;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.Filter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

@Order(1)
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer implements WebApplicationInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] {AppConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        System.out.println("******Servlet Amppings*********");
        return new String[] {"/"};
    }

    protected Filter[] getServletFilters() {
        return null;
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // TODO Auto-generated method stub
        super.onStartup(servletContext);
    }
}

ProductService.java

package com.magicart.poc.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.magicart.poc.service.ProductService;

@RestController
@RequestMapping(path="/product")
public class ProductController {


    @Autowired
    ProductService productService;

    @GetMapping
    public String fetchAllProducts(HttpServletResponse response) {
        System.out.println("a");
        return productService.fetchAllProducts();
    }

    static {
        System.out.println("********Controller bean****************");
    }

}

ProductServiceImpl.java

package com.magicart.poc.service;

public interface ProductService {

    String fetchAllProducts();
}

ProductDAO.java

package com.magicart.poc.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.magicart.poc.dao.ProductDAO;
import com.magicart.poc.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService{


    @Autowired
    ProductDAO productDAO;

    @Override
    public String fetchAllProducts() {
        // TODO Auto-generated method stub
        return productDAO.fetchAllProducts();
    }
}

ProductDAOImpl.java

package com.magicart.poc.dao;

public interface ProductDAO {

    String fetchAllProducts();
}

0 个答案:

没有答案