@Controller @Service配置成的bean

解决方案 »

  1.   

    使用Autowired注解@Service
    public class UserServiceImpl implements UserService {
    //...
    }
    public class UserServiceTest extends BasicTestCase {    @Autowired
        private UserService userService;    @Test
        public void test() {
            //...
        }
    }
      

  2.   

    先检查spring-servlet.xml和application-context.xml里扫描packet的时候是否都有扫描到 如果没问题就在@Controller @Service后面加上(name="xxxBean") 然后再用spring的web工厂getBean就能取到
      

  3.   

    注解生成的Bean一般不会都是类名的首字母小写或者你也可以在注解后定义名字
    @Resource(name = "jdbcTemplate")
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
      

  4.   

    不是在配置文件里用<bean/>标签配置的,
    你把你找到的配置文件铁来看看
      

  5.   

    项目里用 的。 调用videoInfoService = SpringContextHolder.getBean("videoInfoService",
    IVideoInfoService.class);
    相关类package com.ustv.utils;
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Service;
    @Service
    public class SpringContextHolder implements ApplicationContextAware {
        private static ApplicationContext applicationContext;    @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            SpringContextHolder.applicationContext = applicationContext;
        }
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }
        public static Object getBean(String beanName) {
            return applicationContext.getBean(beanName);
        }
        
        public static <T>T getBean(String beanName , Class<T>clazz) {
            return applicationContext.getBean(beanName , clazz);
        }
    }
      

  6.   

    package net.hunau.xiangmu.aijun.module.system.dao.impl;import java.math.BigDecimal;
    import java.util.Date;import net.hunau.xiangmu.aijun.module.system.server.dao.IEmpEntityDao;
    import net.hunau.xiangmu.aijun.module.system.server.shared.domain.EmpEntity;import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations="classpath:application-context.xml")
    public class EmpTest {
    @Autowired
    private IEmpEntityDao empEntityDao;

    @Test
    public void testInsert(){
    EmpEntity record = new EmpEntity();
    record.setDeptno(8L);
    record.setComm(new BigDecimal("55"));
    record.setDeptno(6L);
    record.setHiredate(new Date());
    empEntityDao.insertSelective(record);
    }

    @Test
    public void testSelects(){
    EmpEntity emp = empEntityDao.selectByPrimaryKey(new BigDecimal(7934));
    System.out.println(emp.getDeptno());
    }
    }
      

  7.   

    楼主兄,兄弟没记错的话,应该可以spring的上下文,WebApplicationContext.getBean();可以获取到当前加载的bean哦
      

  8.   

    请教大神,是如何得到bean的,自己看不是很理解。在applicationContext.xml中有如下配置:
    <context:component-scan base-package="com.niit">
    <context:exclude-filter type="regex" expression="com.niit.controller.*"/>
    </context:component-scan>在springServlet.xml文件中
    <context:component-scan base-package="com.niit.controller" />
    请问在java类中,怎么写可以得到需要的bean呢 
      

  9.   

    jsp页面,ctx.getBean("advCallBackService");  这里是怎么得到的ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());    
    AdvCallBackService advCallBackService = (AdvCallBackService)ctx.getBean("advCallBackService");  
    AdvCallBackService   recvAdvAck = (AdvCallBackService)ctx.getBean("advCallBackService");  
      

  10.   

    另外,在AdvCallBackService的实现类AdvCallBackServiceImpl中,有如下代码
    @Service("advCallBackService")
    public class AdvCallBackServiceImpl implements AdvCallBackService {
    private static final Logger logger = LoggerFactory.getLogger(AdvCallBackServiceImpl.class); @Resource
    private CasAdvCallDao advCallDao; @Resource
    private CasCallSourceDao callSourceDao; @Resource
    private CallSourceService callSourceService;请问下是什么意思呢