2015年7月9日
1.必须安装nodejs
2.安装cnpm用cnpm替代npm
地址:http://npm.taobao.org/
安装cnpm:
npm install -g cnpm --registry=https://registry.npm.taobao.org
3、用yarn替代npm
yarn的安装:
第一种方法:参考官方文档https://yarn.bootcss.com/
第二种方法:cnpm install -g yarn 或者 npm install -g yarn
4、搭建React开发环境的第一种方法(老-现在推荐):
https://reactjs.org/docs/create-a-new-react-app.html
1、必须要安装nodejs 注意:安装nodejs稳定版本 教程中的nodejs版本:v8.11.2 教程中的npm版本:v5.6.0
2.安装脚手架工具 (单文件组件项目生成工具) 只需要安装一次
npm install -g create-react-app / cnpm install -g create-react-app
3.创建项目 (可能创建多次)
找到项目要创建的目录:
create-react-app reactdemo
4.cd 到项目里面
cd reactdemo
npm start yarn start运行项目
npm run build yarn build 生成项目
5、搭建React的开发环境的第二种方法(新-未来推荐):
https://reactjs.org/docs/create-a-new-react-app.html
1、必须要安装nodejs 注意:安装nodejs稳定版本 教程中的nodejs版本:v8.11.2 教程中的npm版本:v5.6.0
2.安装脚手架工具并创建项目
找到项目要创建的目录执行:
npx create-react-app reactdemo
4.cd 到项目里面
cd reactdemo
npm start 运行项目(调试)
npm run build 生成项目(发布)
npx介绍:
npm v5.2.0引入的一条命令(npx),引入这个命令的目的是为了提升开发者使用包内提供的命令行工具的体验。
详情:
npx create-react-app reactdemo这条命令会临时安装 create-react-app 包,命令完成后create-react-app 会删掉,不会出现在 global 中。下次再执行,还是会重新临时安装。
npx 会帮你执行依赖包里的二进制文件。
再比如 npx http-server 可以一句话帮你开启一个静态服务器
posted @
2020-04-16 15:25 Terry Zou 阅读(298) |
评论 (0) |
编辑 收藏
@PostConstruct
PostConstruct注释用于在完成依赖项注入以执行任何初始化之后需要执行的方法。必须在类投入使用之前调用此方法。
所有支持依赖注入的类都必须支持此注释。即使类没有请求注入任何资源,也必须调用使用PostConstruct注释的方法。
只有一个方法可以使用此批注进行批注。
应用PostConstruct注释的方法必须满足以下所有条件:除了拦截器之外,方法绝不能有任何参数,在这种情况下它采用Interceptor规范定义的InvocationContext对象。
在拦截器类上定义的方法必须具有以下签名之一:
void <METHOD>(InvocationContext)Object <METHOD>(InvocationContext)抛出异常注意:
PostConstruct拦截器方法不能抛出应用程序异常,但可以声明它抛出检查异常,包括java.lang.Exception,
如果相同的拦截器方法除了生命周期事件之外插入业务或超时方法。
如果PostConstruct拦截器方法返回一个值,容器将忽略它。
在非拦截器类上定义的方法必须具有以下签名:void <METHOD>()应用PostConstruct的方法可以是public,protected,package private或private。
除应用程序客户端外,该方法绝不能是静态的。
该方法可能是最终的。如果该方法抛出一个未经检查的异常,那么该类绝不能投入使用,除非EJB可以处理异常甚至从它们恢复的EJB
然后就会思考问题,这个注释是修饰初始化之后需要执行的方法,那么它和@Autowired、构造函数的执行顺序是什么呢?(当然注释中已经说明了PostConstruct注释用于在完成依赖项注入之后)
@Service
public class BeanA {
@Autowired
private BeanB beanB;
public BeanA() {
System.out.println("这是Bean A 的构造方法");
}
@PostConstruct
private void init() {
System.out.println("这是BeanA的 init 方法");
beanB.testB();
}
}
@Service
public class BeanB {
@PostConstruct
private void init() {
System.out.println("这是BeanB 的init 方法");
}
public BeanB() {
System.out.println("这是Bean B的 构造方法");
}
void testB() {
System.out.println("这是Bean B 的 testB 方法");
}
}
启动后输出:
这是Bean A 的构造方法
这是Bean B的 构造方法
这是BeanB 的init 方法
这是BeanA的 init 方法
这是Bean B 的 testB 方法
所以得到结论: 构造方法 > @Autowired > @PostConstruct
posted @
2020-04-09 15:29 Terry Zou 阅读(299) |
评论 (0) |
编辑 收藏
1、ApplicationContext
Spring的核心,Context我们通常解释为上下文环境。ApplicationContext则是应用的容器。 Spring把Bean(object)放在容器中,需要用就通过get方法取出来。在ApplicationContext接口的众多实现类中,有3个是我们经常用到的(见表1-1),并且使用这3个实现类也基本能满足我们Java EE应用开发中的绝大部分需求。
表1-1 ApplicationContext接口的常用实现类介绍
2、ApplicationEvent
是个抽象类,里面只有一个构造函数和一个长整型的timestamp。其源码如下
public abstract class ApplicationEvent extends EventObject {
/** use serialVersionUID from Spring 1.2 for interoperability */
private static final long serialVersionUID = 7099057708183571937L;
/** System time when the event happened */
private final long timestamp;
/**
* Create a new ApplicationEvent.
* @param source the object on which the event initially occurred (never {@code null})
*/
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
}
/**
* Return the system time in milliseconds when the event happened.
*/
public final long getTimestamp() {
return this.timestamp;
}
}
3、ApplicationListener是一个接口,里面只有一个onApplicationEvent方法。如果在上下文中部署一个实现了ApplicationListener接口的bean,那么每当在一个ApplicationEvent发布到 ApplicationContext时,调用ApplicationContext.publishEvent()方法,这个bean得到通知。类似于Oberver设计模式。
其源码如下:
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);
}
下面举个例子
自定义事件NotifyEvent:
import org.springframework.context.ApplicationEvent;
public class NotifyEvent extends ApplicationEvent {
private String email;
private String content;
public NotifyEvent(Object source){
super(source);
}
public NotifyEvent(Object source,String email,String content){
super(source);
this.email = email;
this.content = content;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
定义监听器NotifyListener:
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
@Configuration
public class NotifyListener implements ApplicationListener<NotifyEvent>{
@Override
public void onApplicationEvent(NotifyEvent event) {
System.out.println("邮件地址:" + event.getEmail());
System.out.println("邮件内容:" + event.getContent());
}
}
单元测试类ListenerTest: import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ServerLauncher.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ListenerTest {
@Autowired
private WebApplicationContext webApplicationContext;
@Test
public void testListener(){
NotifyEvent event = new NotifyEvent("object","abc@qq.com","This is the content");
webApplicationContext.publishEvent(event);
}
}
posted @
2020-04-09 14:47 Terry Zou 阅读(1264) |
评论 (0) |
编辑 收藏
之前用户使用的是3个注解注解他们的main类。分别是@Configuration,@EnableAutoConfiguration,@ComponentScan。由于这些注解一般都是一起使用,spring boot提供了一个统一的注解@SpringBootApplication。
@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
@SpringBootApplication
public class ApplicationMain {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
分开解释@Configuration,@EnableAutoConfiguration,@ComponentScan。
1、@Configuration:提到@Configuration就要提到他的搭档@Bean。使用这两个注解就可以创建一个简单的spring配置类,可以用来替代相应的xml配置文件。
<beans>
<bean id = "car" class="com.test.Car">
<property name="wheel" ref = "wheel"></property>
</bean>
<bean id = "wheel" class="com.test.Wheel"></bean>
</beans>
相当于:
@Configuration
public class Conf {
@Bean
public Car car() {
Car car = new Car();
car.setWheel(wheel());
return car;
}
@Bean
public Wheel wheel() {
return new Wheel();
}
}
@Configuration的注解类标识这个类可以使用Spring IoC容器作为bean定义的来源。@Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为在Spring应用程序上下文中的bean。
2、@EnableAutoConfiguration:能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。
3、@ComponentScan:会自动扫描指定包下的全部标有@Component的类,并注册成bean,当然包括@Component下的子注解@Service,@Repository,@Controller。
posted @
2020-04-09 09:10 Terry Zou 阅读(119) |
评论 (0) |
编辑 收藏
package com.zhihe.xqsh.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import com.zhihe.xqsh.network.ServerErrorException;
import android.accounts.NetworkErrorException;
import android.annotation.SuppressLint;
import android.util.Log;
public class CustomerHttpClient {
private static final String TAG = CustomerHttpClient.class.getSimpleName();
private static DefaultHttpClient customerHttpClient;
private CustomerHttpClient() {
}
public static synchronized HttpClient getHttpClient() {
if (null == customerHttpClient) {
HttpParams params = new BasicHttpParams();
// 设置�?��基本参数
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProtocolParams.setUserAgent(params, "Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83) "
+ "AppleWebKit/553.1(KHTML,like Gecko) Version/4.0 Mobile Safari/533.1");
// 超时设置
/* 从连接池中取连接的超时时�?*/
ConnManagerParams.setTimeout(params, 2000);
ConnManagerParams.setMaxTotalConnections(params, 800);
/* 连接超时 */
HttpConnectionParams.setConnectionTimeout(params, 5000);
/* 请求超时 */
HttpConnectionParams.setSoTimeout(params, 10000);
// 设置我们的HttpClient支持HTTP和HTTPS两种模式
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
// 使用线程安全的连接管理来创建HttpClient
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
// �?��连接数:ConnManagerParams.setMaxTotalConnections(params, 50);
customerHttpClient = new DefaultHttpClient(conMgr, params);
}
return customerHttpClient;
}
/**
* 以get方式提交数据
*
* @param url 提交地址
* @param params 参数
* @return 响应结果
* @throws ServerErrorException 请求失败
* @throws NetworkErrorException 连接失败
*/
public static String get(String url, String params) throws ServerErrorException, NetworkErrorException {
int tryTimes = 0;
NullPointerException ex;
do {
try {
return tryGet(url, params);
} catch (NullPointerException e) {
ex = e;
tryTimes++;
}
} while (tryTimes < 3);
throw ex;
}
/**
* 以get方式提交数据
*
* @param url 提交地址
* @param params 参数
* @return 响应结果
* @throws ServerErrorException 请求失败
* @throws NetworkErrorException 连接失败
*/
public static String tryGet(String url, String params) throws ServerErrorException, NetworkErrorException {
try {
HttpGet request = new HttpGet(url + params);
/*if (LotteryApplication.isCmwap()) {
org.apache.http.HttpHost proxy = new org.apache.http.HttpHost("10.0.0.172", 80, "http");
HttpParams httpParams = new BasicHttpParams();
ConnRouteParams.setDefaultProxy(httpParams, proxy);
request.setParams(httpParams);
}*/
HttpClient client = getHttpClient();
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new ServerErrorException("��������æ�����Ժ�����");
}
HttpEntity resEntity = response.getEntity();
String result = (resEntity == null) ? null : EntityUtils.toString(resEntity, "UTF-8");
return result;
} catch (UnsupportedEncodingException e) {
logw(e.getMessage());
return null;
} catch (ClientProtocolException e) {
logw(e.getMessage());
return null;
} catch (IOException e) {
throw new NetworkErrorException("���Ӳ��ɹ���������������", e);
}
}
private static void logw(String string) {
if (string != null) {
Log.w(TAG, string);
}
}
/**
* 以post方式提交数据
*
* @param url 提交地址
* @param params 参数
* @return 响应结果
* @throws ServerErrorException 请求失败
* @throws NetworkErrorException 连接失败
*/
public static String post(String url, List<NameValuePair> params) throws ServerErrorException, NetworkErrorException {
return post(url, params, null);
}
/**
* 以post方式提交数据
*
* @param url 提交地址
* @param params 参数
* @param soTimeout 响应超时时间,单位毫�?
* @return 响应结果
* @throws ServerErrorException 请求失败
* @throws NetworkErrorException 连接失败
*/
public static String post(String url, List<NameValuePair> params, int soTimeout) throws ServerErrorException,
NetworkErrorException {
HttpParams httpParams;
if (soTimeout <= 0) {
httpParams = null;
} else {
httpParams = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(httpParams, soTimeout);
}
return post(url, params, httpParams);
}
/**
* 以post方式提交数据
*
* @param url 提交地址
* @param params 参数
* @param httpParams http参数
* @return 响应结果
* @throws ServerErrorException 请求失败
* @throws NetworkErrorException 连接失败
*/
public static String post(String url, List<NameValuePair> params, HttpParams httpParams) throws ServerErrorException,
NetworkErrorException {
int tryTimes = 0;
NullPointerException ex;
do {
try {
return tryPost(url, params, httpParams);
} catch (NullPointerException e) {
ex = e;
tryTimes++;
}
} while (tryTimes < 3);
throw ex;
}
/**
* 以post方式提交数据
*
* @param url 提交地址
* @param params 参数
* @param httpParams http参数
* @return 响应结果
* @throws ServerErrorException 请求失败
* @throws NetworkErrorException 连接失败
*/
public static String tryPost(String url, List<NameValuePair> params, HttpParams httpParams) throws ServerErrorException,
NetworkErrorException {
try {
HttpPost request = new HttpPost(url);
if (params != null && params.size() > 0) {
request.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
}
// if (LotteryApplication.isCmwap()) {
// org.apache.http.HttpHost proxy = new org.apache.http.HttpHost("10.0.0.172", 80, "http");
// if (httpParams == null)
// httpParams = new BasicHttpParams();
// ConnRouteParams.setDefaultProxy(httpParams, proxy);
// }
if (httpParams != null)
request.setParams(httpParams);
//Log.v("CS", params.toString());
HttpClient client = getHttpClient();
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
//Log.v("CS", params.toString());
//Log.v("CS", response.getStatusLine().getStatusCode() + "");
request.abort();
throw new ServerErrorException("��������æ�����Ժ�����");
}
if (response.getStatusLine ().getStatusCode () != 200) {
request.abort(); //�ж�����,���������Կ�ʼ��һ������
return null;
}
HttpEntity resEntity = response.getEntity();
String result = (resEntity == null) ? null : EntityUtils.toString(resEntity, "UTF-8");
//Log.v("CS", params.toString() + "||||" + result);
return result;
} catch (UnsupportedEncodingException e) {
logw(e.getMessage());
return null;
} catch (ClientProtocolException e) {
logw(e.getMessage());
return null;
} catch (IOException e) {
throw new NetworkErrorException(e.getMessage(), e);
//throw new NetworkErrorException("连接不成功,请检查网络设�?, e);
}
}
@SuppressLint("SdCardPath")
public static String download(String url) throws ServerErrorException, NetworkErrorException {
try {
//Log.i("http-download", url);
HttpPost request = new HttpPost(url);
HttpClient client = getHttpClient();
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new ServerErrorException("��������æ�����Ժ�����");
}
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
if (is == null)
throw new ServerErrorException("stream is null ");
String fileExt = url.substring(url.lastIndexOf(".") + 1, url.length()).toLowerCase();
String fileName = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
File tempFile = new File("/sdcard/" + fileName + "." + fileExt);
if (!tempFile.exists())
tempFile.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
byte[] buf = new byte[1024];
int ch;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
}
fileOutputStream.flush();
fileOutputStream.close();
return tempFile.getAbsolutePath();
} catch (UnsupportedEncodingException e) {
logw(e.getMessage());
return null;
} catch (ClientProtocolException e) {
logw(e.getMessage());
return null;
} catch (IOException e) {
throw new NetworkErrorException(e.getMessage(), e);
}
}
/**
* 清空cookie
*/
public static void clearCookie() {
if (customerHttpClient != null)
customerHttpClient.getCookieStore().clear();
}
/**
* 清除指定cookie
*
* @param name cookie名称
*/
public static void clearCookie(String name) {
if (customerHttpClient == null)
return;
BasicClientCookie expiredCookie = new BasicClientCookie(name, "null");
expiredCookie.setExpiryDate(new Date(System.currentTimeMillis() - 1000));
customerHttpClient.getCookieStore().addCookie(expiredCookie);
}
}
posted @
2015-07-13 22:10 Terry Zou 阅读(267) |
评论 (0) |
编辑 收藏
http://yunpan.cn/ccdbTgQaYa4U7
posted @
2015-07-13 11:04 Terry Zou 阅读(135) |
评论 (0) |
编辑 收藏
private Drawable img_time_filter,img_time_filter_selected ;
//过滤器TextView中显示的图片
img_time_filter = getResources().getDrawable(R.drawable.time_filter);
//调用setCompoundDrawables时,必须调用Drawable.setBounds()方法,否则图片不显示
img_time_filter.setBounds(0, 0, img_time_filter.getMinimumWidth(), img_time_filter.getMinimumHeight());
img_time_filter_selected = getResources().getDrawable(R.drawable.time_filter_selected);
img_time_filter_selected.setBounds(0, 0, img_time_filter_selected.getMinimumWidth(), img_time_filter_selected.getMinimumHeight());
tv_filterTime.setCompoundDrawables(img_time_filter_selected, null, null, null);
tv_filterTime.setTextColor(getResources().getColor(R.color.white));
rl_filterTime.setBackgroundColor(getResources().getColor(R.color.red));
tv_filterTime.setCompoundDrawables(img_time_filter, null, null, null);
rl_filterTime.setBackgroundColor(getResources().getColor(R.color.white));
lv_filterTime.setVisibility(View.INVISIBLE);
posted @
2015-07-09 00:04 Terry Zou 阅读(201) |
评论 (0) |
编辑 收藏