spring框架总结

一、反射的概念

1.反射的概念

反射就是在运行期间动态的获取的类的基本信息(类的属性,构造方法,普通方法)动态的调用对象的发现,极大地提高了java代码的灵活性

2.反射的三种创建方式

获取Class的方式:
		1.Class c = Class.forName(className);
		2.Class c2 = Test.class;
		3.Test t = new Test();
		Class c = t.getClass();

二、Spring 的概念

1.Spring的概念

Spring是一个轻量级的IoC和AOP容器框架。也是是为Java应用程序提供基础性服务的一套框架

2.Spring的作用

a.提供松耦合 解耦的使用

b.目的是用于简化企业应用程序的开发,它使得开发者只需要关心业务需求。

三、spring的IOC的概念

1.loc 的概念

a.IOC就是控制反转,指创建对象的控制权转移给Spring框架进行管理

b.由Spring根据配置文件去创建实例和管理各个实例之间的依赖关系,对象与对象之间松散耦合,也利于功能的复用。
c.使用java的反射机制,根据配置文件在运行时动态的去创建对象以及管理对象,并调用对象的方法的。

2.loc的注解

注解使用在类的上面

@Service("studentService")//相当于创建了一个对象 
public class StudentServiceImpl implements StudentService {
}
loc的注解
@Component
@Repository(dao)
@Service(service)
@Controller(servelt)

相当于使用xml的

<bean id="student" class="org.lanqiao.entity.Student"/>

使用bean相当于创建了一个student的对象 由于id已经确定该对象的名字 所以类的名字的变化不会影响创建的对象 也就是降低了对象与对象之间的依赖关系

使用配置类

 @Configurable //表明该类是一个配置类  主要是用于配置 用来代替xml
@ComponentScan({"org.lanqiao"})//加入扫描,扫描该类的注解
public class AppConfig {
/* @Bean
 //等价于<bean id= class=/>
   public Student student(){
       return new Student();
   }
   @Bean
    public Grade grade(){
       return new Grade();
   }*/
}

使用配置类 注意点 我们自己创建的类可以使用@Component来创建对象 但是jar包的我们不能使用注解的方式可以采用@Bean的方式创建对象

测试类的使用

 @Test//单元测试
    public void test(){
// 使用配置类 
    AnnotationConfigApplicationContext ac = new 
    AnnotationConfigApplicationContext(AppConfig.class);// 使用配置类 
//使用xml的  配置类和Xml二选一
       ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
//使用getBean创建一个 studentService的对象
       StudentService studentService = (StudentService) ac.getBean("studentService");
//调用 studentService这的对象的方法
       studentService.insertStudent(new Student());
      
   }

总结

1.loc控制反转就是 创建对象的控制权交给spring框架来处理

2.创建对象的方式 xml使用<bean id="student" class="org.lanqiao.entity.Student"/>

注解使用@Component  @Repository(dao)   @Service(service)   @Controller(servelt)

@Lazy 懒加载 只有使用到了才会加载  @Scop Singleton单例模式  prototype多例模式

3.扫描类中使用的注解 xml 使用  

<context:component-scan base-package="org.lanqiao"/>

配置类使用

@ComponentScan({"org.lanqiao"})//加入扫描,扫描该类的注解

4.测试类  xml 使用

ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

配置类使用

AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

四、Spring的DI的概念

Dl依赖注入的前提是ioc创建了对象才能使用

1.使用xml

<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="druidDataSource"/>

<bean id="Student" class="org.lanqiao.Student">

<property name="grade" ref="Grade"/>

</bean>

使用注解

@Autowired//bytype 更具类型来注入
@Qualifier("studentService") //若类型相同加这个更据名字来注入
@Primary//优先级 类型才相同 studentDao前加这个 先会被注入
@Resource  更具名字来注入

@Value 基本值注入

1.定义属性文件 info.prop  key--value

2.导入属性文件@propertySource("class path:info.prop")

3.使用

@Value("${name}")

String name;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值