顯示具有 Spring 標籤的文章。 顯示所有文章
顯示具有 Spring 標籤的文章。 顯示所有文章

2022年1月25日 星期二

Spring 3.03 RestController 亂碼

 Spring 3.03 RestController 亂碼

古蹟維修真的會搞死人


<?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:mvc="http://www.springframework.org/schema/mvc"   
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    default-lazy-init="true" default-autowire="byName">
    
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
    
    <!-- Annotation support -->
    <context:component-scan base-package="io.samchen.web.controller" />

    <mvc:annotation-driven />

</beans>

2020年5月22日 星期五

spring trannsactional

spring trannsactional

透彻的掌握 Spring 中@transactional 的使用

https://www.ibm.com/developerworks/cn/java/j-master-spring-transactional-use/index.html

2020年5月9日 星期六

spring jpa

spring jpa

named query

https://attacomsian.com/blog/spring-data-jpa-named-queries

Spring JPA 参考手册

https://vincentmi.gitbooks.io/spring-jpa-reference-zh/jpa_repository.html

JPA JPQL查詢語法

https://matthung0807.blogspot.com/2018/06/jpa-jpql.html

JPA 2.1 將native sql的query relust型態轉換成POJO的方法

http://saminjava.blogspot.com/2017/06/jpa-21-native-sqlquery-relustpojo.html

2020年3月24日 星期二

Spring5 Mybatis3

Spring 5 + Spring MVC 5 + MyBatis 3 的 Maven 项目集成
https://blog.csdn.net/qq_32596527/article/details/83302826

2020年3月22日 星期日

Spting AOP log

Spting AOP log
利用 Spring AOP 機制 做 log

https://matthung0807.blogspot.com/2019/02/springbootspring-aoplog.html

2020年1月23日 星期四

Spring 初探 1.DI (dependency Injection) 依賴注入

1.DI (dependency Injection)  依賴注入
目的 達到 鬆耦合(loose coupling)

DI 取到的物件為 Design Pattern Singleton 模式
參考:
https://openhome.cc/Gossip/DesignPattern/SingletonPattern.htm

沒有Spring的世界,萬物(件)皆得new()一下
package course.c01;

public class HelloWorldBean {
    public String sayHello() {
        return "Hello World";
    }
}
package course.c01.di.xmlBased;
import course.c01.HelloWorldBean;

public class HelloWorldBeanMain {
    public static void main(String[] args) {
        HelloWorldBean helloWorldBean1 = new HelloWorldBean();
        System.out.println(helloWorldBean1.sayHello());
        
        HelloWorldBean helloWorldBean2 = new HelloWorldBean();
        System.out.println(helloWorldBean2.sayHello());
        
        System.out.println(
            "helloWorldBean1 == helloWorldBean2 :" +
            (helloWorldBean1 == helloWorldBean2));
    }
}
Hello World
Hello World
helloWorldBean1 == helloWorldBean2 :false

有Spring的世界,new() 不見了
<?xml version="1.0" encoding="UTF-8"?>
<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="helloWorldBean" class="course.c01.HelloWorldBean" />
</beans>
<!-- course/c01/di/xmlBased/applicationContext.xml -->
package course.c01.di.xmlBased;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import course.c01.HelloWorldBean;

public class HelloWorldBeanMain {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "course/c01/di/xmlBased/applicationContext.xml");
        
        HelloWorldBean helloWorldBean1 = context.getBean(HelloWorldBean.class);
        System.out.println(helloWorldBean1.sayHello());
        
        HelloWorldBean helloWorldBean2 = context.getBean(HelloWorldBean.class);
        System.out.println(helloWorldBean2.sayHello());
        
        System.out.println(
                "helloWorldBean1 == helloWorldBean2 :" +
                (helloWorldBean1 == helloWorldBean2));
    }
}
一月 23, 2020 2:58:51 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
資訊: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@581cdfc2: startup date [Thu Jan 23 14:58:51 CST 2020]; root of context hierarchy
一月 23, 2020 2:58:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from class path resource [course/c01/di/xmlBased/applicationContext.xml]
Hello World
Hello World
helloWorldBean1 == helloWorldBean2 :true

看到這裡,就可以知道Spring控管的物件,絕對不可以寫 變數 Feld,不然會有悲劇
package course.c01;

public class HelloWorldBean {
    private String name;
    
    public void setName(String name) {
        this.name = name;
    }
    public String sayHello() {
        return "Hello World" + this.name;
    }
}
package course.c01.di.xmlBased;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import course.c01.HelloWorldBean;

public class HelloWorldBeanMain {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "course/c01/di/xmlBased/applicationContext.xml");
        
        HelloWorldBean helloWorldBean1 = context.getBean(HelloWorldBean.class);
        helloWorldBean1.setName("SameChen.io");
        System.out.println(helloWorldBean1.sayHello());
        
        HelloWorldBean helloWorldBean2 = context.getBean(HelloWorldBean.class);
        System.out.println(helloWorldBean2.sayHello());
        
        System.out.println(
                "helloWorldBean1 == helloWorldBean2 :" +
                (helloWorldBean1 == helloWorldBean2));
    }
}
一月 23, 2020 3:30:26 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
資訊: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4fa666bf: startup date [Thu Jan 23 15:30:26 CST 2020]; root of context hierarchy
一月 23, 2020 3:30:26 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from class path resource [course/c01/di/xmlBased/applicationContext.xml]
Hello World SameChen.io
Hello World SameChen.io
helloWorldBean1 == helloWorldBean2 :true
Spring除了XML配置外,還可以使用java類別配置
package course.c01.di.javaBased;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import course.c01.HelloWorldBean;

@Configuration
public class HelloWorldBeanConfig {

    @Bean
    public HelloWorldBean getHelloWorldBean() {
        return new HelloWorldBean();
    }
}
package course.c01.di.javaBased;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import course.c01.HelloWorldBean;

public class HelloWorldBeanMain {

    public static void main(String[] args) {
        ApplicationContext context = 
                   new AnnotationConfigApplicationContext(HelloWorldBeanConfig.class);
        
        HelloWorldBean helloWorldBean1 = context.getBean(HelloWorldBean.class);
        helloWorldBean1.setName("SameChen.io");
        System.out.println(helloWorldBean1.sayHello());
        
        HelloWorldBean helloWorldBean2 = context.getBean(HelloWorldBean.class);
        System.out.println(helloWorldBean2.sayHello());
        
        System.out.println(
                "helloWorldBean1 == helloWorldBean2 :" +
                (helloWorldBean1 == helloWorldBean2));

    }

}
一月 23, 2020 3:44:53 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
資訊: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@432b71eb: startup date [Thu Jan 23 15:44:53 CST 2020]; root of context hierarchy
Hello World SameChen.io
Hello World SameChen.io
helloWorldBean1 == helloWorldBean2 :true