Spring中注解分析@component

 

@Component("studentDaoTest")
public class StudentDaoTest extends BaseTest {
	protected Logger logger = Logger.getLogger(StudentDaoTest.class);

	@Resource(name = "studentDao")
	private IStudentDao studentDao;
	
	@Rollback(false)
	public void testInsert() {
		StudentBo stu = new StudentBo();
		stu.setName("kkk40");
		stu.setAge(39);
		studentDao.insert(stu);
		throw new RuntimeException();
	}

	@Test
	public void testSelect() {
		QueryParam<StudentBo> param = new QueryParam<StudentBo>();
		StudentBo stu = new StudentBo();
		param.setObject(stu);
		param.setPageNum(2);
		param.setPageSize(3);
		List<StudentBo> list = studentDao.findList(param);
		Gson son = new Gson();
		System.out.println("aaa");
		Assert.assertEquals(9l, ((Page<StudentBo>) list).getTotal());
		System.out.println(son.toJson(list));
		System.out.println(list.size());
	}
	public static void main(String[] args) {
	
		String s = "[{'name':'stu','age':'18'},{'name':'stu','age':'18'}]";
		Gson gson =new Gson();
		//传入type转换的demo
		List<StudentBo> li=gson.fromJson(s,  new TypeToken<ArrayList<StudentBo>>(){}.getType());
		
		System.out.println(li.get(0).getName());
	}
}


在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释

这里就是说把这个类交给Spring管理,重新起个名字叫userManager,由于不好说这个类属于哪个层面,就用@Component

阅读更多

没有更多推荐了,返回首页