ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring注解相关的使用

2021-09-14 23:02:56  阅读:163  来源: 互联网

标签:Car Spring cars public context import 相关 class 注解


目录

一、引入外部属性文件配置数据库连接池

1、创建外部properties文件

2、添加druid的pom依赖

3、添加druid.properties

4、创建beans.xml

二、注解的使用

1、非注解的方法

2、注解的方法

 三、依赖注入

 四、配置注解类


一、引入外部属性文件配置数据库连接池

1、创建外部properties文件

2、添加druid的pom依赖

        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.24</version>
</dependency>

3、添加druid.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/t2
name=root
password=123456

4、创建beans.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:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
       xmlns:uitl="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context-4.2.xsd
       "
       >
          <context:property-placeholder location="classpath:druid.properties"></context:property-placeholder>
             <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
             	 <property name="driverClassName" value="${driverClassName}"></property>
       <property name="url" value="${url}"></property>
       <property name="username" value="${name}"></property>
       <property name="password" value="${password}"></property>
             	
             </bean>
</beans>

二、注解的使用

1、非注解的方法

主要通过<bean id=“”class=“”></bean>创建类的对象。

先创建一个对象Car类

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class Car {
private String brand;
private String car;
private double price;
private int maxpeed;
public Car() {
	System.out.println("无参的构造方法");
}
public Car(String brand,String car,double price,int maxpeed) {
	System.out.println("有参的构造方法");
	this.brand=brand;
	this.car=car;
	this.price=price;
	this.maxpeed=maxpeed;
}
}

通过<bean>利用构造方法实现对象的创建

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cars" class="com.DJX.bean.Car">
<constructor-arg name="brand" value="123"></constructor-arg>
<constructor-arg name="car" value="ddd"></constructor-arg>
<constructor-arg name="price" value="123"></constructor-arg>
<constructor-arg name="maxpeed" value="20"></constructor-arg>
</bean>
</beans>

测试类

	@Test
  public void test() {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		Car cars =   context.getBean("cars",Car.class);
		System.out.println(cars.getCar()+"\t"+cars.getBrand()+"\t"+cars.getMaxpeed()+"\t"+cars.getPrice()+"\t");
	}

效果展示 

2、注解的方法

注解是代码的特殊符号,格式:@注解名称

使用注解的目的:简化xml配置

在classpath中扫描组件:组件扫描(component scanning):spring能够从classpath下自动扫描。侦测和实例化具有特定注解的组件。

扫描com.DJX.bean包,下面的所有类

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

 通过注释来对Car类赋值


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

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Component//标识为实体类

public class Car {
	@Value("321")
private String brand;
	@Value("汽车")
private String car;
	@Value("123")
private double price;
	@Value("20")
private int maxpeed;
public Car() {
	System.out.println("无参的构造方法");
}
public Car(String brand,String car,double price,int maxpeed) {
	System.out.println("有参的构造方法");
	this.brand=brand;
	this.car=car;
	this.price=price;
	this.maxpeed=maxpeed;
}
}

 测试类

	@Test
  public void test() {
		ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
		Car cars =   context.getBean(Car.class);
		System.out.println(cars.getCar()+"\t"+cars.getBrand()+"\t"+cars.getMaxpeed()+"\t"+cars.getPrice()+"\t");
	}

效果展示:

 三、依赖注入

给出三个类分别是DeptDao、DeptService、DeptAction

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DeptDao {
	@Autowired
public void add() {
	System.out.println("Dao的add");
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.DJX.Dao.DeptDao;
@Service
public class DeptService {
	@Autowired
private DeptDao deptDao;
public void add() {
	System.out.println("service层的add");
	deptDao.add();
}

}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.DJX.service.DeptService;
@Controller
public class DeptAction {
	@Autowired
	private DeptService service;
	public void add() {
		System.out.println("DeptAction的add");
		service.add();
	}
}

DeptAction依赖DeptService依赖DeptDao ,通过@Autowired注入

控制层:controller

业务层:service

数据访问层:respository

普通的:component四个方法效果一样,只是这样取名方便辨认

当然仍然扫描com.DJX包,下面的所有类

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

测试类

	public void test8() {
		ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
		DeptAction deptAction=context.getBean(DeptAction.class);
		deptAction.add();
//		DeptService deptService = context.getBean(DeptService.class);
//		deptService.add();
	}

效果展示

 四、配置注解类

不在通过xml实现配置,因为如果通过注释的话都是需要进行包的扫描,那我们为何不直接创建一个配置类呢?

所以下面我们通过配置类来实现

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration//告诉SPring是配置
@ComponentScan(basePackages= {"com.DJX"})//扫描的信息
public class SpringConfig {

}

因为我们采用的是配置类所以我们new的方法要改变,通过AnnotationConfigApplicationContext(SpringConfig.class)创建对象

测试类

@Test
	public void test5() {
		ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
		Dept dept=context.getBean(Dept.class);
		System.out.println(dept.getId()+"\t"+dept.getName());
	}

效果展示

 

 

标签:Car,Spring,cars,public,context,import,相关,class,注解
来源: https://blog.csdn.net/qq_48241564/article/details/120297908

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有