2019.03.11(spring)

本文详细介绍了Spring框架的使用步骤,从添加依赖开始,到配置applicationContext.xml,再到实体类的编写与注入,最后通过测试验证。涵盖懒加载、scope、DI注入、注解等核心概念,以及如何重构Dao层和服务层。

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

第一步:添加依赖

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.11.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging    -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.3</version>
    </dependency>
  </dependencies>

第二步:.添加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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    		 http://www.springframework.org/schema/aop 
     		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           ">
    <!-- spring配置 -->
    <!-- 
		一个bean就是一个被spring容器管理的类   管理他的生命周期 IOC
			class  就是被spring管理的类
			id是bean的标志  id必须唯一  id的值一般情况下是类名的首字母小写
	 -->


</beans>

第三步:编写一个实体类

public class User {
    private String name;
    private int age;
    //该注释用于给department赋值
    @Resource(name = "Department")
	private Department department
    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.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;
    }
    
    public void say(){
        System.out.println("我被使用");
    }
    
    public void init(){
        System.out.println("我被创建");
    }
    
    public void destory(){
        System.out.println("我被销毁");
    }
}

第四步:把该类交给spring容器

设置对象创建时调用的方法,对象被销毁时调用的方法

	<!--init-method对应被创建时我们调用的方法,destroy-method对应被销毁时我们使用的方法-->
    <bean id="user" class="com.njdf.bean.User" init-method="init" destroy-method="destory"></bean>
    <bean id="user1" class="com.njdf.bean.User" init-method="init" destroy-method="destory" ></bean>

第五步:进行测试

public static void main(String[] args) {
        //加载配置文件,并加载所有bean标签和@Component所对应类的空参构造,同时运行对象被创建时调用的方法
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //提取id为user的bean标签的对象
        User user = (User) context.getBean("user");
        user.say();
        //销毁对象
        context.close();
    }

运行结果:
在这里插入图片描述

懒加载

懒加载lazy-init不写的话就是default等价于false,在加载xml文件时被加载,占用系统内存,耗费时间,如果改成true则不会在加载xml文件时被加载

<bean id="user1" class="com.njdf.bean.User" init-method="init" destroy-method="destory" lazy-init="true"></bean>

运行结果:
在这里插入图片描述

scope

<!--scope的使用singlon对应一个bean对应一个实例,prototype一个bean对应多个实例,默认不写为singlon-->
    <bean id="user2" class="com.njdf.bean.User" lazy-init="false" scope="singleton"></bean>

DI注入

DI注入用的是有参构造

<!--DI注入-->
    <bean id="user3" class="com.njdf.bean.User" >
        <constructor-arg index="0" value="Tony"></constructor-arg>
        <constructor-arg index="1" value="23"></constructor-arg>
    </bean>

下标注入

下标注入用的是set方法

	<bean id="d1" class="com.njdf.bean.Department">
        <constructor-arg index="0" value="财务部"></constructor-arg>
        <constructor-arg index="1" value="1"></constructor-arg>
    </bean>
	<bean id="user4" class="com.njdf.bean.User">
        <property name="age" value="18"></property>
        <property name="name" value="zhangsan"></property>
        <property name="list">
            <list>
                <value>"123</value>
                <value type="int">121</value>
            </list>
        </property>
        <property name="d">
                <ref bean="d1"></ref>
        </property>
    </bean>

注解

第一步:在applicationContext.xml配置文件里面添加注解扫描器

在这里插入图片描述

<context:component-scan base-package="com.njdf"></context:component-scan>

第二步:在User实体类上添加注解@Component("")

import org.springframework.stereotype.Component;
import java.util.List;
@Component("user10")
public class User {

第三步:在测试类中运行

用学过的知识改写Dao层和service层

如果一个类加上了@Component注解,就会进行以下法则
@Component相当于
有三个注解相当于Component
dao层:@Repository
Service层:@Service
Controller层:@Controller

Dao层接口

public interface UserDao {
    public void add();
}

Dao层实现

@Repository("UserDaoImpl")
public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
        System.out.println("添加学生");
    }
}

Service层接口

public interface UserService {
    public void add();
}

Service层实现

@Service("UserServiceImpl")
public class UserServiceImpl implements UserService {
    //这两个注释用于区分这个实现UserDao接口的对象的类是哪一个
    @Autowired
    @Qualifier("UserDaoImpl")
    private UserDao udi;
    @Override
    public void add() {
        this.udi.add();
    }
}
一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值