Spring入门(注解方式)

一概述

这里写图片描述

二代码

1.导入jar包
2.类路径下创建applicationContext.xml配置文件

<?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 http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- <context:annotation-config /> -->

    <!-- 下面的配置作用是指定spring扫描的包,它包含了context:annotation-config的功能,所以在开发中一般就不需要在配置上面的 -->
    <context:component-scan base-package="cn.itheima" />

</beans>


3.编写类,类上加注解

package com.blueSky.annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(value="prototype")  //设置为多例
public class HelloSpringAnno {
    @Value("你好,spring注解开发")
    private String content;

    @Autowired
    private HelloSpringAnno_02 helloSpringAnno_02;


    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public HelloSpringAnno_02 getHelloSpringAnno_02() {
        return helloSpringAnno_02;
    }
    public void setHelloSpringAnno_02(HelloSpringAnno_02 helloSpringAnno_02) {
        this.helloSpringAnno_02 = helloSpringAnno_02;
    }

    public void printContent(){
        System.out.println(content+"      "+helloSpringAnno_02.getContent());
    }
}
package com.blueSky.annotation;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class HelloSpringAnno_02 {
    @Value("你好,spring!")
    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

4.applicationContext.xml文件中配置包扫描(扫描注解),扫描的是bean

<?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 http://www.springframework.org/schema/context/spring-context.xsd">


    <!-- 下面的配置作用是指定spring扫描的包,它包含了context:annotation-config的功能,所以在开发中一般就不需要在配置上面的 -->
    <context:component-scan base-package="com.blueSky.annotation" />

</beans>


5.测试

package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.blueSky.annotation.HelloSpringAnno;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpringJunitTest {

    @Autowired
    private HelloSpringAnno helloSpringAnno;

    @Test
    public void test(){
        helloSpringAnno.printContent();
    }

}

这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring是一个开源的Java开发框架,它提供了一种简化Java应用开发方式。下面是一些入门Spring开发的步骤: 1. 环境搭建:首先,你需要安装Java开发环境和构建工具,比如JDK和Maven或Gradle。然后,你可以从Spring官方网站上下载Spring框架的发布版本。 2. 创建项目:使用Maven或Gradle创建一个新的Spring项目。在项目配置文件中添加所需的Spring依赖项,比如spring-context和spring-web等。 3. 配置Spring容器:创建一个配置文件(通常是XML文件),并在其中定义Spring容器。Spring容器负责管理应用程序中的所有对象。你可以在配置文件中定义各种bean,包括服务、数据访问对象(DAO)、控制器等。 4. 开发业务逻辑:编写业务逻辑代码,并将其组织为Spring bean。你可以使用注解或XML配置来定义bean,并使用依赖注入(DI)来管理bean之间的关系。 5. 实现控制器:创建控制器来处理用户请求。你可以使用Spring MVC框架来实现Web应用程序的控制器层。 6. 运行应用程序:使用构建工具将项目打包成可执行的JAR或WAR文件。然后,你可以在本地或远程服务器上运行应用程序。 这只是一个简单的入门指南,Spring框架非常强大且功能丰富,还有很多其他的特性和模块可以学习和使用。你可以通过阅读Spring官方文档和参考书籍来深入学习Spring开发。希望对你有帮助!如果你有任何更具体的问题,我可以帮助你解答。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值