导航:首页 > 文件管理 > spring注解的配置文件

spring注解的配置文件

发布时间:2024-07-05 05:45:55

1. spring的常用注解是什么

Spring Boot常用注解

1、@SpringBootApplication

替代 @SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan

2、@ImportAutoConfiguration

导入配置类,一般做测试的时候使用,正常优先使用@EnableAutoConfiguration

3、@SpringBootConfiguration

替代@Configuration

4、@ImportResource

将资源导入容器

5、@PropertySource

导入properties文件

6、PropertySources

@PropertySource 的集合

(1)spring注解的配置文件扩展阅读:

Controller 该类为Controller。

RequestMapping 配置方法路径等信息。

ResponseBody 返回值,例如JSON,XML。

PathVariable 获取RESTFUL路径中的值如 /company/{corpId}/dept/{deptId}。

RequestParam 获取Request参数值如xxx?from=index_nav。

Component,Repository,Service。

一般用Repository,service用Service,需要多个service时,一般用Componet。

2. spring怎么配置注解

@Repository注解:

1 package imooc_spring.test.anotation.myrepository;

2

3 import org.springframework.stereotype.Repository;

4

5 /**

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

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

8 *

9 * @author Wei

10 */

11 @Repository("wyl")

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;

2

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(" Hi this is TestObj.SayHi()...");

13 }

14 }

复制代码

@Controller注解闷吵:

复制代码

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Controller;

4

5 @Controller

6 public class UserController {

7 public void execute(){

8 System.out.println(" UserController.execute()...");

9 }

10 }

复制代码

@Repository注解:

复制代码

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Repository;

4

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(" UserRepositoryImpl.save()...");

13 }

14

15 }

复制代码

@Service注解:

复制代码

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Service;

4

5 @Service

6 public class UserService {

7 public void add(){

8 System.out.println(" UserService.add()...");

9 }

10 }

3. SpringBoot项目使用@PropertySource注解加载properties配置文件,但输出对象值为null

不是要加上前缀?
@ConfigurationProperties(prefix = "Example")
@PropertySource("classpath:my.properties")//这是属性文件路径

4. java开发中spring注解的使用方法

注解方式李态配置(简化XML配置文件)
组件自动扫描功能
首先需要在applicationContext.xml中添加<context:component-scan/>
a.扫描Bean组件的注解,替代xml中的<bean>元素的定义。
@Service 用于Service业务组件
@Control 用于Action控制组件
@Respository 用于DAO数据访问组哪源源件
@Component 用于其他组件
Bean组件扫描到容器后,裂羡默认名字为类名(首字母小写)
如果需要自定义名称可以使用@Service("id名")
b.依赖注入的注解标记
@Resource 按名称@Resource(name="id名")
@AutoWired 按名称
@Autowired
@Qualifier("id名")
c.其他注解
@Scope 等价于<bean scope="">
@PostConstruct 等价于<bean init-method="">
@PreDestroy 等价于<bean destroy-method="">

阅读全文

与spring注解的配置文件相关的资料

热点内容
unlockioctl头文件 浏览:309
网站修改后怎么上线 浏览:296
word97工具栏 浏览:294
猫编程网课教学怎么样 浏览:162
linuxmintsougou输入法 浏览:910
贷款大数据被拒绝 浏览:213
哪个网站能看片 浏览:750
大数据在税务应用 浏览:18
等离子个圆怎么编程 浏览:386
运营商的大数据 浏览:764
实战java高并发程序设计视频 浏览:52
程序员改造硬件 浏览:782
js监听enter 浏览:929
硬盘坏了如何把d盘数据导出 浏览:900
a1528的运营商文件在哪 浏览:997
文件怎么还原打开方式 浏览:527
matlab读取ai矢量文件格式 浏览:370
windows7的文件夹组织结构是一种 浏览:230
ie6安装程序无法安装所有组件 浏览:795
贵阳大数据作文 浏览:418

友情链接