首页 > 其他分享 >Spring-8-注解(对象的创建)

Spring-8-注解(对象的创建)

时间:2022-10-31 15:38:14浏览次数:44  
标签:Context 对象 创建 age 创建对象 context Spring org 注解


文章目录

  • ​​注解的di​​
  • ​​1.使用注解第一步加入maven的依赖(spring-context)​​
  • ​​2.在类里面加spring的注解​​
  • ​​创建对象1.@Context(value="对象名")==@Context("对象名")或者@Context 这里的对象名默认是类名的首字母小写​​
  • ​​创建对象2.@Repository(用在持久层的类上面)表示创建dao对象,dao对象是可以访问数据库的​​
  • ​​创建对象3.@Service用在业务层的类上面):放在service的实现类上面,是做事物的,可以有事务等功能​​
  • ​​创建对象4.@Controller(用在控制器类上面),创建控制器上面的,能接受参数,显示处理结果​​
  • ​​上面的和@Context使用语法相同,但是后三个还有额外功能,给项目对象分层的​​
  • ​​3.在配置文件中加入扫描器的标签​​
  • ​​1.使用多次组件扫描器​​
  • ​​2.使用分割符号;或者,分割多个包名​​
  • ​​3.使用指定的父包(不建议)​​

注解的di

1.使用注解第一步加入maven的依赖(spring-context)

<!--spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
</dependencies>

2.在类里面加spring的注解

创建对象1.@Context(value=“对象名”)==@Context(“对象名”)或者@Context 这里的对象名默认是类名的首字母小写

创建对象2.@Repository(用在持久层的类上面)表示创建dao对象,dao对象是可以访问数据库的

创建对象3.@Service用在业务层的类上面):放在service的实现类上面,是做事物的,可以有事务等功能

创建对象4.@Controller(用在控制器类上面),创建控制器上面的,能接受参数,显示处理结果

上面的和@Context使用语法相同,但是后三个还有额外功能,给项目对象分层的

package org.example.ba01;

import org.springframework.stereotype.Component;

/**
* @compatent:创建对象的,等同于<bean>功能
*
* 属性value就是对象的名称,也就是bean的id值
* value值是唯一的,创建的对象在整个Spring容器中只有一个
* 位置:在类的是上面
* 等同于<bean id="myStudent" class="ba01.student" />
* 需要配置文件resource里面
*/
@Component(value = "myStudent")
public class Studnet {

private String name ;
private int age;

@Override
public String toString() {
return "Studnet{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

3.在配置文件中加入扫描器的标签

1.使用多次组件扫描器

<context:component-scan base-package=“org.example.ba01”/>
<context:component-scan base-package=“org.example.ba02”/>
<context:component-scan base-package=“org.example.ba03”/>

2.使用分割符号;或者,分割多个包名

<context:component-scan base-package=“org.example.ba01;org.example.ba02;org.example.ba03”/>

3.使用指定的父包(不建议)

<context:component-scan base-package=“org.example”/>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--申明组件扫描器-->
<!--component-scan组件就是java对象 -->
<!--component-scan 的工作方式,Spring会按照 base-package的路径通过注解创建对象,或者给对象赋值 -->
<!--加入 component-scan这个和配置文件会加入一些东西 -->
<context:component-scan base-package="org.example.ba01"/>
</beans>

Spring-8-注解(对象的创建)_创建对象

主要学习的注解
1.@component
2.@Respotory
3.@Service
4.@Controller
5.@Value
6.@Resource


标签:Context,对象,创建,age,创建对象,context,Spring,org,注解
From: https://blog.51cto.com/u_15854304/5809885

相关文章

  • Solr 8.11入门教程(2)创建core
    新建core添加core命令添加使用命令比较简单~$bin/solrcreate-cmytest[core名称]这样就添加完了。CoreAdmin就可以看到了。手动添加手动添加相对复杂一些,需要提前创建目......
  • 对象创建,表结构就会自动生成
    1.目录2.User.java1packagecom.itheima.domain;23publicclassUser{45privateIntegerid;6privateStringname;7privateIntegerage;8......
  • Spring Security 认证流程
    SpringSecurity认证流程   本文以用户名/密码验证方式为例,讲解SpringSecurity的认证流程,在此之前,需要你了解SpringSecurity用户名/密码认证的基本配置。Sp......
  • Springboot整合mybatis-plus-2增删改查
    文章目录​​一、插入​​​​1.主键id配置自增的​​​​2.时间等自动填充​​​​1.直接在数据字段设置即可​​​​2.使用ioc反射原理​​​​二、改(update)​​​​三、......
  • Springboot 整合 thymeleaf-1
    文章目录​​thymeleaf百叶香​​​​一、添加依赖​​​​1.创建项目的时候引入依赖​​​​2.直接在pom里面添加依赖​​​​二、建立页面html并添加命名空间​​​​三......
  • VUE项目创建
    首先你的电脑安装了node.js,并进行配置vue创建一个新的项目首先cmdd:然后cd:/路径(这里是项目保存的路径)vueinitwebpack项目名vue修改端口号config的index.js第一步......
  • Spring-13-监听器
    格外加入三个依赖<!--servlet依赖--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0<......
  • Spring-12-事务
    文章目录​​1.使用注解写​​​​1.在ApplicationContext.xml写入代码​​​​2.在实现接口类的公共方法上面写注解​​​​方法一:​​​​方法二:​​​​2.使用在配置中实......
  • Spring-14-Aspect总结-Spring和Mybatis整合
    ......
  • Springboot之Shiro权限管理
    文章目录​​Springboot之Shiro权限管理介绍​​​​1.shiro有三个核心api(接口)​​​​2.如何使用:​​​​第一步导入shiro与springboot整合依赖​​​​第二步配置shiro......