首页 > 编程语言 > 详细

spring基础

时间:2020-03-20 11:19:45      阅读:52      评论:0      收藏:0      [点我收藏+]

Spring

优点:开源免费的框架(容器)。轻量级,非入侵式框架。控制反转(ioc),面向切面编程(aop)。支持事务处理,对框架整合的支持

IOC本质

控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法,

hello Spring

  1. 导包

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.4.RELEASE</version>
      </dependency>
          <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.2.4.RELEASE</version>
      </dependency>
  2. 创建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:util="http://www.springframework.org/schema/util"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
           https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
    </beans>
  3. 创建接口以及实现类

  4. 创建服务层接口以及实现类

    public class UserServiceImpl implements UserService {
      private UserDao userDao;
    ?
      public void setUserDao(UserDao userDao) {
          this.userDao = userDao;
      }
    ?
      @Override
      public void getUser() {
          userDao.getUser1();
      }
    }
  5. 在beans.xml中编写配置,注册成功类旁边会有小叶子??

    <bean id="oracl" class="com.lt.pojo.oracl"/>
    <bean id="sql" class="com.lt.pojo.sql"/>
    <bean id="UserServiceImpl" class="com.lt.sevice.UserServiceImpl">
          <property name="UserDao" ref="oracl"/>
    </bean>            
  6. 创建测试类测试

Ioc创建对象的方式

  1. 使用无参构造创建对象,默认。

  2. 有参创建对象三种方法

    1. 下标赋值

       <bean id="user" class="com.lt.pojo.User">
            <constructor-arg index="0" value=""/>
      </bean>

       

    2. 类型赋值

      <bean class="com.lt.pojo.User" id="user">
            <constructor-arg type="java.lang.String" value=""/>
      </bean>

       

    3. 参数名赋值

      <bean class="com.lt.pojo.User" id="user">
                  <constructor-arg name="name" value=""/>
            </bean>

三种注入方式

  1. 构造器注入

  2. 通过set注入

    1. 依赖:bean创建的对象依赖于容器

    2. 注入: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:util="http://www.springframework.org/schema/util"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
            <bean class="com.lt.dao.Address" name="address"/>
            <bean class="com.lt.dao.Student" name="student">
                  <property name="name" value="lllt"/>
                  <property name="address" ref="address"/>
                  <property name="book">
      ?
                          <array>
                                <value>1</value>
                                <value>2</value>
                                <value>3</value>
                                <value>4</value>
                          </array>
                  </property>
      <property name="hobbies">
            <list>
                  <value>11</value>
                  <value>22</value>
                  <value>33</value>
                  <value>44</value>
            </list>
      </property>
      <property name="card">
            <map>
                  <entry key="1" value="1"/>
            </map>
      </property>
                  <property name="games">
                          <set>
                                <value>4</value>
                                <value>5</value>
                                <value>6</value>
                                <value>7</value>
                          </set>
                  </property>
                  <property name="wife" >
                          <null/>
                  </property>
                  <property name="info">
                          <props>
                                <prop key="1">4</prop>
                                <prop key="2">3</prop>
                                <prop key="3">2</prop>
                                <prop key="4">1</prop>
                          </props>
                  </property>
            </bean>
      </beans>

       

  3. 拓展方式注入

    p命名空间  set注入属性 xmlns:p="http://www.springframework.org/schema/p"
    c命名空间 构造器 mlns:c="http://www.springframework.org/schema/c"

By**自动装配

byname

<bean class="com.lt.dao.Student" name="student" autowire="byName"/> id唯一

bytype

<bean class="com.lt.dao.Student" name="student" autowire="byType"/> //保证全局class唯一

使用注解自动装配

要使用需配置

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

byName

会在容器上下文寻找和自己对象set后面的值对应的bean id

如:set Dog dog(){} 则引入狗的id必须为dog而不能是其他

byType

会在容器上下文寻找和自己对象属性类型相同的bean

如,set Dog dog(){} 有Dog这个类就行 id可以不对应

@Autowired

作用在被引用对象的私有方法上,xml只需要引入类即可。如

    @Autowired
  private Dog dog;
  @Autowired
  private Cat cat;
   
  <bean class="com.lt.pojo.Cat" id="cat123"/>
      <bean class="com.lt.pojo.Dog" id="dog"/>
<bean class="com.lt.pojo.People" id="people" >

拓展

@Autowired(required = false)
//如果显示定义是false 表明该类属性可以为空

@Qualifier

如果自动装配环境比较复杂,无法通过一个@Autowired去实现,我们可以使用@Qualifier(value=“xxx”)去配置使用,指定唯一对象注入

@Resource(name = "")

最高级,会先通过byName去查找,再通过byType查找,后边可以跟name= 去查找具体的

@Component

相当于注册,如

<bean class="com.lt.pojo.People" id="people" >

必须配合使用

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

衍生注解:

dao:@Repository

service:@Service

controller @Controller

@Value("")

可以在属性字段赋值

不使用xml只用java配置

@Component
@Data
public class User {
  @Value("lt")
  private String name;
}
?
?
?
@Configuration
public class UserConf {
  @Bean
  public User getUser(){
      return new User();
  }
}
?
?
public static void main(String[] args) {
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(UserConf.class);
      User getUser = context.getBean("getUser", User.class);
      System.out.println(getUser.getName());
  }

aop实现方式

方式一:使用spring的api接口

public class AfterLog implements AfterReturningAdvice {
  public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
      System.out.println(o1.getClass().getName()+" "+method.getName());
  }
}
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
public class Log implements MethodBeforeAdvice {
  public void before(Method method, Object[] objects, Object o) throws Throwable {
      System.out.println(o.getClass().getName()+" "+method.getName());
  }
}
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
<bean class="com.lt.mapper.UserServiceImpl" name="userService"/>
  <bean class="com.lt.log.AfterLog" name="afterLog"/>
  <bean class="com.lt.log.Log" name="log"/>
  <aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.lt.mapper.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
      <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
  </aop:config>

方式二:自定义来实现aop

public class Diy {
  public void befor(){
      System.out.println("-------------------------------------------");
  }   public void after(){
      System.out.println("-------------------------------------------");
  }
}
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
<bean class="com.lt.mapper.UserServiceImpl" name="userService"/>
  <bean class="com.lt.Diy.Diy" name="diy"/>
  <aop:config>
      <aop:aspect ref="diy">
          <aop:pointcut id="point" expression="execution(* com.lt.mapper.UserServiceImpl.*(..))"/>
          <aop:before method="befor" pointcut-ref="point"/>
          <aop:after method="after" pointcut-ref="point"/>
      </aop:aspect>
  </aop:config>

方式三:使用注解来实现

@Aspect
public class Diy {
  @Before("execution(* com.lt.mapper.UserServiceImpl.*(..))")
  public void befor(){
      System.out.println("-------------------------------------------");
  }
  @After("execution(* com.lt.mapper.UserServiceImpl.*(..))")
  public void after(){
      System.out.println("-------------------------------------------");
  }
}
?
?
<bean class="com.lt.Diy.Diy" name="diy"/>
  <aop:aspectj-autoproxy/> //不要忘记开自动
   
   

 

使用Spring AOP需要导入包

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.5</version>
</dependency>
?

 

spring基础

原文:https://www.cnblogs.com/ltdh/p/12530359.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!