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 param = new QueryParam();
		StudentBo stu = new StudentBo();
		param.setObject(stu);
		param.setPageNum(2);
		param.setPageSize(3);
		List list = studentDao.findList(param);
		Gson son = new Gson();
		System.out.println("aaa");
		Assert.assertEquals(9l, ((Page) 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 li=gson.fromJson(s,  new TypeToken>(){}.getType());
		
		System.out.println(li.get(0).getName());
	}
}


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

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

你可能感兴趣的:(java-spring)