spring通过注解配置bean

本文详细介绍了Spring框架中@Component、@Controller、@Service及@Repository等注解的使用方法,并阐述了这些注解在不同层(如Controller层、Service层、Repository层)的应用方式以及如何通过这些注解将类注册为Spring容器中的bean。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
注意点

  • @Component是一个基本注解,一个受spring管理组件,如果@Repository,@Controller,@Service,三个注解都没有给value命名(—不给值@Controller,给值得为@Controller(“UserController”)—)那么,必须要有@Component注解例如:
    没有@Component就无法进行识别
    Component层
package com.wenhe.annotation;
import org.springframework.stereotype.Component;

@Component
public class TestObject {

}

Controller层

package com.wenhe.controller;

import org.springframework.stereotype.Controller;

@Controller
public class UserController {
	
	public void execute(){
		System.out.println("UserController execute...");
	}
}

service层

package com.wenhe.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
	
	public void add(){
		System.out.println("userservice  add....");
	}
	
}	

@Repository层Dao

package com.wenhe.dao;

import org.springframework.stereotype.Repository;

@Repository("UserRspository")
public class UserRspositoryImpl implements UserRspository {

	@Override
	public void save() {
		System.out.println("UserRspositoryImpl   save(111");
	}

}

测试结果:
.getBean(“userController”);中的命名必须是该类的首字母小写其他 不变,如下:

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationcontext.xml");
		
		TestObject to = (TestObject)ctx.getBean("testObject");
		System.out.println(to);
		
		UserController usercontroller = (UserController)ctx.getBean("userController");
		System.out.println(usercontroller);
		
		
		UserService userService = (UserService)ctx.getBean("userService");
		System.out.println(userService);
		
		UserRspositoryImpl UserRspository = (UserRspositoryImpl)ctx.getBean("userRspositoryImpl");
		System.out.println(UserRspository);

不需要@Component的则如下:

  • 需要每一层的注解都要给定命名

Service

package com.wenhe.service;

import org.springframework.stereotype.Service;

@Service("UserService")//要自定义命名
public class UserService {
	
	public void add(){
		System.out.println("userservice  add....");
	}
	
}	

Controller

package com.wenhe.controller;

import org.springframework.stereotype.Controller;

@Controller("UserController")
public class UserController {
	
	public void execute(){
		System.out.println("UserController execute...");
	}
}

Repository

package com.wenhe.dao;

import org.springframework.stereotype.Repository;

@Repository("UserRspository")
public class UserRspositoryImpl implements UserRspository {

	@Override
	public void save() {
		System.out.println("UserRspositoryImpl   save(111");
	}

}

.getBean(“UserController”);必须为自己定义的名

	ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationcontext.xml");
		
		
		UserController usercontroller = (UserController)ctx.getBean("UserController");
		System.out.println(usercontroller);
		
		
		UserService userService = (UserService)ctx.getBean("UserService");
		System.out.println(userService);
		
		UserRspositoryImpl UserRspository = (UserRspositoryImpl)ctx.getBean("UserRspository");
		System.out.println(UserRspository);

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值