spring怎么配置注解

如题所述

@Repository注解:

1 package imooc_spring.test.anotation.myrepository;

3 import org.springframework.stereotype.Repository;

5 /**

6  * 指定id,默认为dAO,即类名首字母小写,如果指定了名称那么只能ctx.getBean(指定名称)来获取bean

7  * 这个例子里就只能通过ctx.getBean("wyldao)来获取DAO 的实例了;

8  * 

9  * @author Wei

10  */

11 @Repository("wyldao")

12 public class DAO {

13     /**

14      * 返回x和y的乘积

15      * 

16      * @param x

17      * @param y

18      * @return x*y

19      */

20     public int multi(int x, int y) {

21         return x * y;

22     }

23 }

复制代码

@Component 注解:

复制代码

1 package imooc_spring.test.anotation;

3 import org.springframework.stereotype.Component;

4 /**

5  * Component 注解

6  * @author Wei

7  *

8  */

9 @Component

10 public class TestObj {

11     public void SayHi(){

12         System.out.println("\nHi this is TestObj.SayHi()...");

13     }

14 }

复制代码

@Controller注解:

复制代码

1 package imooc_spring.test.anotation;

3 import org.springframework.stereotype.Controller;

5 @Controller

6 public class UserController {

7     public void execute(){

8         System.out.println("\nUserController.execute()...");

9     }

10 }

复制代码

@Repository注解:

复制代码

1 package imooc_spring.test.anotation;

3 import org.springframework.stereotype.Repository;

5 //@Repository

6 @Repository("wyl_repo")

7 public class UserRepositoryImpl implements IUserRepository {

8 //模拟持久化层

9     @Override

10     public void save() {

11         // TODO Auto-generated method stub

12         System.out.println("\nUserRepositoryImpl.save()...");

13     }

14 

15 }

复制代码

@Service注解:

复制代码

1 package imooc_spring.test.anotation;

3 import org.springframework.stereotype.Service;

5 @Service

6 public class UserService {

7     public void add(){

8         System.out.println("\nUserService.add()...");

9     }

10 }

温馨提示:答案为网友推荐,仅供参考